Created
November 29, 2011 22:50
-
-
Save crimsonknave/1406979 to your computer and use it in GitHub Desktop.
printing hashes nicely in jruby
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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