"puts, if and unless" VS "nil"

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
*

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.

···

On Wed, Nov 10, 2010 at 2:26 PM, Edmond Kachale <edmond.kachale@baobabhealth.org> 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?

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

···

2010/11/10 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:

On Wed, Nov 10, 2010 at 2:26 PM, Edmond Kachale > <edmond.kachale@baobabhealth.org> 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.

========================================
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/

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

Jesus.

···

On Wed, Nov 10, 2010 at 2:40 PM, Robert Klemme <shortcutter@googlemail.com> wrote:

2010/11/10 Jesús Gabriel y Galán <jgabrielygalan@gmail.com>:

On Wed, Nov 10, 2010 at 2:26 PM, Edmond Kachale >> <edmond.kachale@baobabhealth.org> 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 $