Skip to content

Instantly share code, notes, and snippets.

@crimsonknave
Created November 29, 2011 22:50
Show Gist options
  • Save crimsonknave/1406979 to your computer and use it in GitHub Desktop.
Save crimsonknave/1406979 to your computer and use it in GitHub Desktop.
printing hashes nicely in jruby
# Jruby not running in 1.9 mode squashes hashes, ruby doesn't so we'll do a bit of unsquashing for those in jruby land
my_hash = {1 => 2, 3 => "asdf", 4 => :whee}
# In jruby you would get
# puts my_hash
# 123asdf4whee
# with this you get
# {1 => 2, 3 => "asdf", 4 => "whee"}
puts "{#{my_hash.to_a.collect{|k,v| "#{k} => #{'"' if v.class == String || v.class == Symbol}#{v}#{'"' if v.class == String || v.class == Symbol}"}.join(", ")}}"
# If you want to print symbols like symbols do the following
puts "{#{my_hash.to_a.collect{|k,v| "#{k} => #{'"' if v.class == String}#{':' if v.class == Symbol}#{v}#{'"' if v.class == String}"}.join(", ")}}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment