Why does this code produce a nil following a Proc.call?

Asked
Viewd117

0
C:ruby 1.8.6 (2008-08-11 patchlevel 287) [i386-mswin32]
gt;irb
irb(main):001:0> s = Proc.new { puts "Hello" }
=> #<Proc:[email protected](irb):1>
irb(main):002:0> s.call
Hello
=> nil

What causes the nil?

12321

1 ответов

3

s does not return a value, As @sepp2k points out in the comment, puts returns nil, so nil automatically becomes the return value since that is the last statement in the proc.

It just gets printed to the terminal as the return value of the last statement, similar to that cryptic output after you assign the proc to s.

  • More accurately: s returns the value that is returned by puts and puts returns nil.

    sepp2k16 сентября 2009, 17:38
  • I haven’t used Ruby much, I was just going by paradigms from other languages. Thanks and answer updated.

    Mark Rushakoff16 сентября 2009, 19:29