Hash Keys and Values in General Ruby

Create a new Hash

node = Hash.new

Create a new Hash of Hash

node[:one] = Hash.new

Insert Values into your Hash of Hash

node[:one][:object] = "number1"
node[:one][:block] = "number2"

Print Key and Value

node[:one].each_pair do |k,v|
  puts k
  puts v
end

Print Keys only

node[:one].keys.each do |key|
  puts key
end

Print Values Only

node[:one].values.each do |value|
  puts value
end
Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s