Problem with return value

Hi
In this code :

def test_method(name)
puts name
3+5
end

return_value = test_method(“James”)
puts return_value

why when the last statement executed , “puts” does not return “nil” and
return 8?

On 06/08/2010 17:23, Amir E. wrote:

return_value = test_method(“James”)
puts return_value

why when the last statement executed , “puts” does not return “nil” and
return 8?

Because in Ruby, the last statement executed in a block is the block’s
value. That goes for your test_method’s return value, and any other
type of block you use.

On Aug 6, 2010, at 12:27 PM, Matthew B. wrote:

any other type of block you use.


Matthew B.

Actually, 3+5 is the last statement in your test_method. Are you
surprised that the value is 8?

-Rob

Rob B.
[email protected] http://AgileConsultingLLC.com/
[email protected] http://GaslightSoftware.com/

On Fri, Aug 6, 2010 at 12:23 PM, Amir E. [email protected]
wrote:

why when the last statement executed , “puts” does not return “nil” and
return 8?

irb(main):001:0> def test_method(name)
irb(main):002:1> rv = puts(name)
irb(main):003:1> 3+5
irb(main):004:1> rv
irb(main):005:1> end
=> nil
irb(main):006:0> return_value = test_method(“James”)
James
=> nil
irb(main):007:0> puts return_value
nil
=> nil

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs