Created
July 19, 2019 00:01
-
-
Save caioertai/afc6f367d571c66d7699f50d2a38aa59 to your computer and use it in GitHub Desktop.
Ruby Day 4 Lecture Notes - Hash & Symbols
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 0 1 2 3 | |
students = [ "Peter", "Mary", "George", "Emma" ] | |
student_ages = [ 24 , 25 , 22 , 20 ] | |
# Write a program that prints this: | |
# Peter is 24 years old | |
students.each_with_index do |student, index| | |
age = student_ages[index] | |
p "#{student} is #{age} years old" | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# CRUD | |
# Array CRUD | |
# cities = ["Paris", "London"] | |
# C: cities << "Rio de Janeiro" | |
# R: cities[2] | |
# U: cities[2] = "Melbourne" | |
# D: cities.delete_at(2) / cities.delete("London") | |
# Hash CRUD | |
paris = { | |
"country" => "France", | |
"population" => 2211000 | |
} | |
# C: | |
paris["monument"] = "Pain au Chocolat" | |
p paris["monument"] | |
# R: | |
p paris["country"] | |
# U: | |
paris["country"] = "Moon" | |
p paris["country"] | |
# D: | |
paris.delete("country") | |
p paris |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
paris = { | |
"country" => "France", | |
"population" => 2211000 | |
} | |
# Let's output this using Hash#each: | |
# The key X stores the value Y | |
paris.each do |key, value| | |
p "The key #{key} stores the value #{value}" | |
end | |
# BE CAREFUL!!! If you don't use both parameters on the | |
# block you'll get an array as the iterated object. | |
paris.each do |object| | |
p object # => ["country", "France"] | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Our old hash: | |
paris = { | |
"country" => "France", | |
"population" => 2211000 | |
} | |
# Old Ruby syntax: | |
paris = { | |
:country => "France", | |
:population => 2211000 | |
} | |
# New Ruby syntax: | |
paris = { | |
country: "France", | |
population: 2211000 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
paris = { | |
"country" => "France", | |
"population" => 2211000, | |
"star_monument" => "Tour Eiffel" | |
} | |
# Checking keys: | |
p paris.has_key?("country") | |
p paris.has_key?("language") | |
# Print the keys of this hash: | |
p paris.keys | |
# Print the values of this hash: | |
p paris.values |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Rebuilding the tag method of yesterday | |
# We're cheating on the blocks. Sorry! | |
def tag(tag_name, attributes = {}) | |
attributes_string = attributes.map { |name, value| "#{name}='#{value}'" }.join(" ") | |
"<#{tag_name} #{attributes_string}>#{yield}</#{tag_name}>" | |
end | |
my_tag = tag("a", href: "http://lewagon.org", class: "btn") do | |
tag("h1") do | |
"Hello" | |
end | |
end | |
p my_tag |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment