Skip to content

Instantly share code, notes, and snippets.

@tobie
Created March 12, 2009 23:33
Show Gist options
  • Save tobie/78355 to your computer and use it in GitHub Desktop.
Save tobie/78355 to your computer and use it in GitHub Desktop.
Ruby p() of common types
p(nil)
#-> nil
p(false)
#-> false
p(true)
#-> true
p('foo')
#-> "foo"
p(123)
#-> 123
p(123.4)
#-> 123.4
p(/foo/)
#-> /foo/
p(Time.now)
#-> Fri Mar 13 00:21:41 +0100 2009
p([0, 1, [2, 3]])
#-> [0, 1, [2, 3]]
{:foo => 123}
#-> {:foo=>123}
# self-containing arrays
a = [1, 2, 3]
a << a
p(a)
#-> [1, 2, 3, [...]]
# self-containing hashes
h = {:foo => 123}
h[:bar] = h
#-> {:foo=>123, :bar=>{...}}
# custom class
class Foo
def initialize
@bar = 123
@foo = 456
end
end
p(Foo)
#-> Foo
p(Foo.new)
#-> #<Foo:0x8e7d8 @foo=456, @bar=123>
p([].method('reverse'))
#-> #<Method: Array#reverse>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment