How to view the structure of a hash

I keep finding myself trying to decipher the structure of hash objects
and when I just print the hash, I get pages of scrolling text because
it’s showing all elements and text in the hash, and that is of very
little help. Can someone tell me some ways of better viewing the
structure of my hash objects?

Syntax from memory, so please forgive errors…

$hash.each{|k,v|
puts v
}

You’ll want to do a bit more to make it readable, but this gives you
control on a per-object level…

HTH,
Carey

Would awesome_print( https://github.com/michaeldv/awesome_print ) work?
-Nick K.

This doesn’t really change much in my opinion because the values are
so big.

eggman2001 писал 09.12.2011 04:21:

I keep finding myself trying to decipher the structure of hash
objects
and when I just print the hash, I get pages of scrolling text because
it’s showing all elements and text in the hash, and that is of very
little help. Can someone tell me some ways of better viewing the
structure of my hash objects?

require ‘pp’ # Pretty printer
pp hash

Notice the “you’ll want to do a bit more to make it readable” part.

Peter Z. already suggested the way to go.

Via pp. pretty print.

require ‘pp’
pp hash

This doesn’t really change much in my opinion because
the values are so big.

If your hash is really huge, I would randomly select only a subset of
these and pretty print them.

And also use Wirble if you do this in IRB, it really is colouring
nicely.

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

What exactly do you want to discover about the hash? What it’s values
are? It’s size? Number of elements?

On 12/08/2011 09:14 PM, eggman2001 wrote:

objects and when I just print the hash, I get pages of scrolling
text because it’s showing all elements and text in the hash, and
that is of very little help. Can someone tell me some ways of
better viewing the structure of my hash objects?


frosty
public key: unluckyfrosty.net/public_key
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.11 (GNU/Linux)

iQEcBAEBAgAGBQJO4X36AAoJEMWQxuocG7AkB6wIAIKZkfGf2mYCE9aKfmSuMIY3
c8/S7wIB2OsRvkGydyv2C38xMKsy/EtdGb/U2deNXOvE7eLQgSjQV70xb8CrFIkt
Ei8CPr3Pvbb21KYSle4+b2nbrFgh4/e+oo66CubMgqGUji3e8SSjlRQiPRd4+UK+
fZyS/Fg/lnAGWUDDFwPllRRigg52iEGJyRGOgHwrR/bWVVxTSg9lZ/cCFIxZDR+b
FwUH1d6fY9AlBp21oUrtmtaoZJMe1bFiRaqABQ4ePGu6JWKG23KEKGEKUDvk5aBJ
/x91Kt1D92luWI1HlTnHDNi38ZExxZg5SoukEcv+VRZzphL5LwsOjAbq0lFtEJQ=
=jxrX
-----END PGP SIGNATURE-----

Thank you all for the responses. I’ll try these all out.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs