Simply question

Can’t understand:

coding: cp1251

x = puts ‘bbb’
puts x

out needs to be:

=> bbb
=> nil

But nil is not returned(bbb displayed),only empty line instead of nil is
on screen.

ruby v.1.9.2-p180

On 07/10/11 09:56, itankgo itankgo wrote:

=> nil

But nil is not returned(bbb displayed),only empty line instead of nil is
on screen.

ruby v.1.9.2-p180

Try

x = puts ‘bbb’

puts x.inspect

Sam

Thanks

On Thu, Oct 6, 2011 at 2:02 PM, Sam D. [email protected] wrote:

x = puts ‘bbb’

puts x.inspect

The docs for IO#print, IO#puts, and OI#inspect help understand what’s
going on here. puts outputs myobject.to_s followed by an added
newline. x is nil, the string representation of nil is ‘’, so puts x
correctly outputs “\n”. As Sam noted, the inspect method is what’s
needed for these situations.

IIf you pull up your irb, it does show =>NIL
The empty line is NIL but running it in a command prompt window will
only output an empty line.
To prove this put (puts x.to_i)
It will output the value of that line which is “0”

----- Original Message -----
From: itankgo itankgo [email protected]
To: ruby-talk ML [email protected]
Cc:
Sent: Thursday, October 6, 2011 4:56 PM
Subject: Simply question

Can’t understand:

coding: cp1251

x = puts ‘bbb’
puts x

out needs to be:

=> bbb
=> nil

But nil is not returned(bbb displayed),only empty line instead of nil is
on screen.

ruby v.1.9.2-p180

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs