Ruby/Hash/Print Hash

Материал из Wiki.crossplatform.ru

Версия от 17:10, 26 мая 2010; (Обсуждение)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Содержание

Print a hash with PP module

require "pp"
h = {}
h[:name] = "Robert"
h[:nickname] = "Bob"
h[:age] = 43
h[:email_addresses] = {:home => "bob@example.ru",
                       :work => "robert@example.ru"}
pp h[:email_addresses]
# {:home=>"bob@example.ru", :work=>"robert@example.ru"}
pp h



Printing a Hash

h = {}
h[:name] = "Robert"
h[:nickname] = "Bob"
h[:age] = 43
h[:email_addresses] = {:home => "bob@example.ru",
                       :work => "robert@example.ru"}
puts h
puts h[:email_addresses]
p h[:email_addresses]
p h



Print out a nested hash

require "pp"
 
h = {}
h[:name] = "Robert"
h[:nickname] = "Bob"
h[:age] = 43
h[:email_addresses] = {:home => "bob@example.ru",
                       :work => "robert@example.ru"}
PP::pp(h, $stderr, 50)



using puts on a hash in Ruby doesn"t result in as nice a display as it does for arrays

pizza = {"first_topping" => "pepperoni", "second_topping" => "sausage"}
puts pizza
p pizza