0

I write a ruby program that prints 3 different values of variable a with different data types:

a = 5 puts a a = true puts a a = 1.325 return a puts a 

In this case, last value didn't print. When i remove return a, program prints a last value, 1.325. But how?

1

1 Answer 1

1

return controls program flow, calling it will exit the current method and pass the value of a into whatever expression called it.

Usually you would not write a method with an unconditional return and more code afterward, because that code is not reachable. The puts in your example will never be called. Just move it to before the return expression if you want it to run.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.