Skip to content

Instantly share code, notes, and snippets.

@arpit
Created July 15, 2016 02:38
Show Gist options
  • Save arpit/135e93dea1b54fda56fe6bf529bcaf10 to your computer and use it in GitHub Desktop.
Save arpit/135e93dea1b54fda56fe6bf529bcaf10 to your computer and use it in GitHub Desktop.
#create empty hash
myhash = {}
# or
myh = Hash.new
# keys no value
info = { :name => nil, :address => nil }
#set value of keu
info[:name] = "arpit"
#
info.each do |k,v|
puts "The key is #{k} and the value is #{v}"
end
# hashes in arrays
one = { :name => "arpit", :city => "philly" }
two = { :name => "dana", :city => "philly" }
three = { :name => "jack", :city => "new york" }
people = [one, two, three]
people.each do |c|
puts c[:city]
end
# array in hash
one[:hobbies] = [ "art", "movies", "music"]
# supermarket #of aisles abcd each has random products
#products are bread donuts cornflakes chocolate
products = ["bread", "dountes", "cornflakes", "chocolate"]
aisles = { "a" => [], "b" => [], "c" => [], "d" => [] }
products.each do |p|
aisles[ p[0] ] << p
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment