Ruby: How to make IRB print structure for Arrays and Hashes
You can either use the inspect
method:
a=["value1", "value2", "value3"]
puts a.inspect
Or, even better, use the pp (pretty print) lib:
require 'pp'
a=["value1", "value2", "value3"]
pp a
Best way to pretty print a hash
require 'pp'
pp my_hash
Use pp
if you need a built-in solution and just want reasonable line breaks.
Use awesome_print if you can install a gem. (Depending on your users, you may wish to use the index:false
option to turn off displaying array indices.)
Printing array of arrays, each internal on separate line
Use Kernel#p rather than Kernel#puts.
('a'..'c').to_a.combination(2).each { |a| p a }
["a", "b"]
["a", "c"]
["b", "c"]
Note that, while Array#combination without a block returns an enumerator, you don't have to convert it to an array before each
'ing it.
IRB and large variables?
Or, can I print each one on a separate line, tabbed-in depending on depth?
Use pp (pretty print):
require 'pp'
very_long_hash = Hash[(1..23).zip(20..42)]
pp very_long_hash
# Prints:
{1=>20,
2=>21,
3=>22,
4=>23,
5=>24,
6=>25,
7=>26,
8=>27,
9=>28,
10=>29,
11=>30,
12=>31,
13=>32,
14=>33,
15=>34,
16=>35,
17=>36,
18=>37,
19=>38,
20=>39,
21=>40,
22=>41,
23=>42}
How do you print Ruby hashes as arrays? I keep getting hex values
The issue isn't that you're not using a hash correctly, it's that you are printing out the Game
object. You may need to make your own method to grab the exact attributes you would like printed. For example
def print_assists(game)
puts game.RawStatsDto.assists
end
It is also worth noting that when printing objects for debugging purposes, it is almost always better to use p
instead of puts
e.g. p game
search through an array of hashes and print out result
You can do something like this
@email = "thomas@fake.nl"
user = @users.find {|u| u[:email] == @email}
if user
puts "Welcome #{user[:name]}"
else
puts "Your account does not exist try again!"
end
Hash inspection in IRB?
Object.inspect
The method is usually used for printing object structure.
IRB (apparently) not inspecting hashes correctly
The issue here is that invoking h["test"]
doesn't actually insert a new key into the hash - it just returns the default value, which is the array that you passed to Hash.new
.
1.8.7 :010 > a = []
=> []
1.8.7 :011 > a.object_id
=> 70338238506580
1.8.7 :012 > h = Hash.new(a)
=> {}
1.8.7 :013 > h["test"].object_id
=> 70338238506580
1.8.7 :014 > h["test"] << "blah"
=> ["blah"]
1.8.7 :015 > h.keys
=> []
1.8.7 :016 > h["bogus"]
=> ["blah"]
1.8.7 :017 > h["bogus"].object_id
=> 70338238506580
1.8.7 :019 > a
=> ["blah"]
The hash itself is still empty - you haven't assigned anything to it. The data isn't present in the hash - it's present in the array that is returned for missing keys in the hash.
Related Topics
How to Add a New Action to the Existing Controller
Why Does Ruby on Rails Use Http://0.0.0.0:3000 Instead of Http://Localhost:3000
How to Execute Ruby Template Files (Erb) Without a Web Server from Command Line
What Does "Wrong Number of Arguments (1 for 0)" Mean in Ruby
Why Alias_Method Fails in Rails Model
Match String That Doesn't Contain a Specific Word
Error When Doing Rake Db:Migrate on Heroku
How to Create a Full Audit Log in Rails for Every Table
Is There a Less Intrusive Alternative to Rspec's 'Should_Receive'
Class Method VS Constant in Ruby/Rails
Get Class Location from Class Object