Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Hashes are sort of like JavaScript objects or Python dictionaries in storing key value pairs.
- # 1st way to create hashes
- pets = {
- "Stevie" => "cat",
- "Bowser" => "hamster",
- "Kevin Sorbo" => "fish"
- }
- # print pets hashes using .each
- pets.each { |x, y| puts "#{x}: #{y}" }
- # 2nd way to create using the Hash keyword with new
- fruits = Hash.new
- fruits["red"] = "apple" # add each key value pair individually
- fruits["green"] = "pear"
- fruits["yellow"] = "mango"
- # print using .each and do
- # When iterating over hashes, we need two placeholder variables to represent each key/value pair.
- fruits.each do |color, fruit|
- puts "#{color}: #{fruit}"
- end
- # Array type 1
- languages = ["HTML", "CSS", "JavaScript", "Python", "Ruby"]
- languages.each{ |element| puts element}
- # Array type 2 - 2D form
- s = [["ham", "swiss"], ["turkey", "cheddar"], ["roast beef", "gruyere"]]
- # to print each element in each sub array, put them inside the curly brackets
- s.each{ |sub_array|
- sub_array.each {|x|
- puts x
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement