"puts, if and unless" VS "nil"

E

Edmond Kachale

[Note: parts of this message were removed to make it a legal post.]

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
*
 
J

Jesús Gabriel y Galán

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)) {
line = rb_str_new2("nil");
}

It specifically checks for nil arguments and prints "nil" in that case.

Jesus.
 
R

Robert Klemme

2010/11/10 Jes=FAs Gabriel y Gal=E1n said:
Rubysters,

I was sharing with some friends how to use if and unless. I wrote this
statement: *puts nil if nil*
and I got =3D>nil. (Which implies nothing was printed.)

When I wrote: *puts nil unless nil*
I got:
nil
=3D> 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

=A0if (NIL_P(argv)) {
=A0 =A0 =A0 =A0 =A0 =A0line =3D rb_str_new2("nil");
=A0 =A0 =A0 =A0}

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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
nil
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.9.1p429 (2010-07-02 revision 28523) [i386-cygwin]

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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


--=20
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
 
J

Jesús Gabriel y Galán

2010/11/10 Jes=FAs Gabriel y Gal=E1n said:
Rubysters,

I was sharing with some friends how to use if and unless. I wrote this
statement: *puts nil if nil*
and I got =3D>nil. (Which implies nothing was printed.)

When I wrote: *puts nil unless nil*
I got:
nil
=3D> 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

=A0if (NIL_P(argv)) {
=A0 =A0 =A0 =A0 =A0 =A0line =3D rb_str_new2("nil");
=A0 =A0 =A0 =A0}

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
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.8.7 (2008-08-11 patchlevel 72) [i386-cygwin]
nil
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
ruby 1.9.1p429 (2010-07-02 revision 28523) [i386-cygwin]

=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D
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 $


Ooops, forgot to mention I was answering for MRI 1.8.7, thanks.

Jesus.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top