From: Robert Klemme Date: 2010-11-10T22:40:39+09:00 Subject: Re: "puts, if and unless" VS "nil" 2010/11/10 Jesús Gabriel y Galán : > On Wed, Nov 10, 2010 at 2:26 PM, Edmond Kachale > wrote: >> Rubysters, >> >> I was sharing with some friends how to use if and unless. I wrote this >> statement: *puts nil if nil* >> and I got =>nil. (Which implies nothing was printed.) >> >> When I wrote: *puts nil unless nil* >> I got: >> nil >> => nil >> Which implies that puts output nil. >> >> My question is what is in *"nil"* that makes "*puts*" print "*nil"*? Or is >> there any explanation to back this behaviour? > > It's in the implementation of puts: > > http://ruby-doc.org/core/classes/IO.src/M002252.html > >  if (NIL_P(argv[i])) { >            line = rb_str_new2("nil"); >        } > > It specifically checks for nil arguments and prints "nil" in that case. This is highly version dependent! 14:38:33 $ allruby -e 'puts nil' CYGWIN_NT-5.1 padrklemme2 1.7.7(0.230/5/3) 2010-08-31 09:58 i686 Cygwin ======================================== ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin] nil ======================================== ruby 1.9.1p429 (2010-07-02 revision 28523) [i386-cygwin] ======================================== jruby 1.4.0 (ruby 1.8.7 patchlevel 174) (2009-11-02 69fbfa3) (Java HotSpot(TM) Client VM 1.6.0_21) [x86-java] nil 14:39:43 $ Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/