hello
why does:
puts “Inspect:#{p(myDog)}”
give LESS verbose results than:
p(myDog)
?
is it that p returns an array, and puts only prints the first element of
arrays?
thanks
hello
why does:
puts “Inspect:#{p(myDog)}”
give LESS verbose results than:
p(myDog)
?
is it that p returns an array, and puts only prints the first element of
arrays?
thanks
On 05/12/10 02:55, Johny W. wrote:
is it that p returns an array, and puts only prints the first element of
arrays?
“p x” is a shorthand for “puts x.inspect”, i.e. it actually does the
printing. The “inspect” method is what actually returns a “verbose”
string. I suspect the intent of your first statement was:
puts “Inspect:#{myDog.inspect}”
If you call p during a string interpolation, you can expect the output
to appear before the whole strong you’ve asked it to print, which is
probably not what you wanted.
On Sat, Dec 4, 2010 at 6:55 PM, Johny W. [email protected] wrote:
why does:
puts “Inspect:#{p(myDog)}”give LESS verbose results than:
p(myDog)
The method #p is meant to be used as a replacement for:
puts obj.inspect
It outputs itself, so you don’t want to interpolate its output like
you’re doing above. Try this:
print "Inspect: "
puts myDog.inspect
… vs …
print "p: "
p myDog
Ben
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs