Skip to content

Instantly share code, notes, and snippets.

@boddhisattva
Last active August 29, 2015 13:56
Show Gist options
  • Save boddhisattva/9081092 to your computer and use it in GitHub Desktop.
Save boddhisattva/9081092 to your computer and use it in GitHub Desktop.
Hashes in Ruby
Total Months via hashes: 8
#{numbers["two"]}
zero
5
#Playin with Hashes..:)
#--------------------------------------------------------------------------------------------------------------------------------
months = Hash.new
months["january"] = 1
months["july"] = 7
total_months = months["january"] + months["july"]
puts("Total Months via hashes: #{total_months} ")
puts "#{months["june"]}"
#note how a value of null is assigned to a hash of months .. as it was defined with no default initial value
#--------------------------------------------------------------------------------------------------------------------------------
#also check how puts is working without the use of parentheses..
numbers = Hash.new{"zero"}
puts '#{numbers["two"]}' # givin it this way won't work.. need to use double quotes...
puts "#{numbers["two"]}"
#--------------------------------------------------------------------------------------------------------------------------------
# usage of hash literals... as key value pairs.. Here we are using symbols.. but even quotes can be used.. as shown above..
week = Hash.new
week = { :sunday => 1, :monday => 2, :"tuesday" => 3, :wednesday => 4, :thursday => 5, :'fri day' => 6, :saturday => 7 }
puts "#{week[:thursday]}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment