okkezSS
November 10, 2010, 2:27pm
1
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?
Edmond
Software Developer | Baobab Health Trust (http://www.baobabhealth.org/ )
|
Malawi
Cell: +265 999 465 137 | +265 881 234 717
*“Leading the Improvement of Health through Information and
Communication
Technology in the Developing World” The *Creed of *Baobab Health
*
ceekays
November 10, 2010, 2:34pm
2
On Wed, Nov 10, 2010 at 2:26 PM, Edmond K.
[email protected] wrote:
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.
Jesus.
ceekays
November 10, 2010, 2:46pm
3
2010/11/10 Jess Gabriel y Galn [email protected] :
nil
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™ Client VM 1.6.0_21) [x86-java]
nil
14:39:43 $
Kind regards
robert
ceekays
November 10, 2010, 6:33pm
4
On Wed, Nov 10, 2010 at 2:40 PM, Robert K.
[email protected] wrote:
I got:
========================================
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™ Client VM 1.6.0_21) [x86-java]
nil
14:39:43 $
Ooops, forgot to mention I was answering for MRI 1.8.7, thanks.
Jesus.