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 Answers1

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.

Neil Slater
  • 26,512
  • 6
  • 76
  • 94