Gavin Kistner
12/27/2006 4:46:00 PM
will wrote:
> book = Book.new("Ruby Tutorial", "Dave", 831)
> book.inspect
Others have mentioned the need to output the value you are returning,
and have suggested:
puts book.inspect
as a way to see that information. I just wanted to add the suggestion
that the 'p' method:
p book
automatically calls #inspect on the object(s) passed, and then spits
them to stdout like puts does. (For comparison, the puts method calls
#to_s on the object(s) passed.) 'p' is very convenient when developing
and testing out your application, as the output sometimes gives you
more insight into the actual format of the data...particularly for
arrays:
irb(main):001:0> a = [ 1, '2', [ '3 4', 5 ], 6 ]
=> [1, "2", ["3 4", 5], 6]
irb(main):002:0> puts a
1
2
3 4
5
6
=> nil
irb(main):003:0> p a
[1, "2", ["3 4", 5], 6]
=> nil
As you can see from that first line, it's the result of #inspect that
irb uses when outputting the result of the last command. (Both puts and
p return nil when finished, which is where those two "=> nil" lines
come from in irb.)