Monday, May 21, 2012

What does the ruby p function do?

The p function takes a variable number of arguments and prints how they would be displayed in the ruby interpreter. This is the same as the string that is returned by the obj.inspect method.

Example: you have an array a = [1,2,3,4] and a string s = 'hello'. a.inspect returns the string [1,2,3,4], and s.inspect returns the string "hello" (including the quotation marks). So p a,s would output:

[1,2,3,4]
"hello"

That's what the ruby p function does.

No comments:

Post a Comment