From: Edmond Kachale Date: 2010-09-03T19:47:51+09:00 Subject: Re: p vs. print Let IRB speak for himself: **** >> x = [2,4,1] => [2, 4, 1] >> p x [2, 4, 1] => nil >> puts x 2 4 1 => nil >> print x 241=> nil >> p x.inspect "[2, 4, 1]" => nil >> puts x.inspect [2, 4, 1] => nil >> print x.inspect [2, 4, 1]=> nil >> p x.to_s "241" => nil >> puts x.to_s 241 => nil >> print x.to_s 241=> nil **** p x = puts x.inspect ceekays ======= Brian Candler wrote: > Abder-Rahman Ali wrote: >> When I tried to display the result using >> >> print ary1 >> >> It did NOT show anything. > > print(x) calls x.to_s to get the string representation, then sends that > to $stdout. > > It looks like the object you have returns an empty string or nil from > its to_s method (which is odd, but you'd have to talk to the author of > the NArray class about that) > > puts(x) is like print(x), but adds a newline if there isn't one at the > end of the string already. > > p(x) is like puts(x.inspect), where x converts an object into a > debugging string form, that is, one which shows its internal state. > > Try this program to see the differences: > > class Foo > def initialize(a) > @a = a > end > def to_s > "here's a string" > end > end > > f = Foo.new(123) > print f > puts f > p f -- Posted via http://www.ruby-forum.com/.