Last active
April 15, 2022 15:57
-
-
Save caioertai/1b711c412f7b29a37a7ea858213984b4 to your computer and use it in GitHub Desktop.
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
# Quick data objects recap: | |
# There are 99 bottles of Indio in Cantina La Llorona. | |
# They cost $20.15 MXN each. The restaurant is currently closed. | |
number_of_bottles = 99 # Integer | |
name_of_the_bar = "La Llorona" # String | |
indio_price = 20.15 # Float | |
is_it_open = false # boolean | |
# Cantina La Llorona has the following beers for sale: | |
# Leopoldina, Backbone, Dogma, Havana Dreams. | |
# Their prices are respectively: | |
# 0 1 2 3 | |
beers_names = ["Leopoldina", "Backbone", "Dogma", "Havana Dreams"] | |
beers_prices = [21.90 , 13.30 , 44.90 , 18.90] |
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
# Write a program that prints beer prices. | |
# 0 1 ... | |
beers_names = ["Leopoldina", "Backbone", "Dogma", "Havana Dreams"] | |
beers_prices = [21.90 , 13.30 , 44.90 , 18.90] | |
# associate objects by using => | |
# key value | |
beers_menu = { | |
"Leopoldina" => 21.90, | |
"Backbone" => 13.30, | |
"Dogma" => 44.90, | |
"Havana Dreams" => 18.90 | |
} | |
# beers_menu = { | |
# "Leopoldina" => { "price" => 21,90, "region" => "Chiapas" } | |
# } | |
# beers_menu["Leopoldina"] # => {"price" => 21,90, "region" => "Chiapas"} | |
# beers_menu["Leopoldina"]["price"] | |
# beers_menu["Leopoldina"]["region"] | |
beers_names.each_with_index do |beer_name, index| | |
puts "#{beer_name} costs $#{beers_prices[index]} MXN" | |
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
puts "=====================" | |
puts "Array CRUD" | |
puts "=====================" | |
# 0 1 2 | |
array = ["Hank", "Bob", "Sheila"] | |
# Create | |
# << / #push / #append | |
array << "Uni" | |
# Read | |
# [index] | |
array[1] | |
# Update | |
# [index] = | |
array[1] = "Diana" | |
# Delete | |
# #delete_at(index) | |
array.delete_at(1) | |
puts "=====================" | |
puts "Hash CRUD" | |
puts "=====================" | |
# Hash literal | |
sao_paulo = { | |
# key value | |
"name" => "São Paulo", | |
"population" => 12_800_000, | |
"main_monument" => "smog" | |
} | |
# "state" -> "São Paulo" | |
# Create | |
# [key] = | |
sao_paulo["state"] = "São Paulo" | |
# Read | |
# [key] | |
sao_paulo["name"] | |
# Update | |
# [key] = ----- for a key that exists | |
sao_paulo["population"] = 12_800_001 | |
# Delete | |
# #delete(key) | |
sao_paulo.delete("main_monument") |
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 => 2_211_000, | |
:monument => "Pain au Chocolat" | |
} | |
sao_paulo = { | |
:country => "Brazil", | |
:population => 12_211_000, | |
:monument => "smog" | |
} | |
# Let's iterate to output the info like this: | |
# The country is France | |
# The population is 2211000 | |
# paris.each do |key, value| | |
# end | |
# key value | |
paris.each do |info_name, info_value| | |
puts "The #{info_name} is #{info_value}" | |
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
paris = { | |
"country" => "France", | |
"population" => 2_211_000, | |
"monument" => "Pain au Chocolat" | |
} | |
# What happens if I access a key that doesn't exist? | |
paris["beach"] => nil | |
# Checking the presence of a key | |
# paris.has_key?("beach") | |
paris.key?("beach") | |
# Return the keys of this hash | |
paris.keys # => ["country", "population", "monument"] | |
# Return the values of this hash | |
paris.values # => ["France", 2211000, "Pain au Chocolat"] |
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
# Unconventional hashes (using strings as keys) | |
paris = { | |
"country" => "France", | |
"population" => 2_211_000 | |
} | |
# Recommended in Ruby way (using symbols as keys) | |
# Symbols to represent identifiers: | |
paris = { | |
:country => "France", | |
:population => 2_211_000 | |
} | |
# Shorthand hash literal (preferred): | |
paris = { | |
country: "France", | |
population: 2_211_000 | |
} |
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
# Accepts 2 or 3 arguments | |
def tag(tag_name, content, attributes = {}) | |
attributes_string = "" | |
attributes.each do |attr_name, attr_value| | |
attributes_string += " #{attr_name}='#{attr_value}'" | |
end | |
return "<#{tag_name}#{attributes_string}>#{content}</#{tag_name}>" | |
end | |
puts tag("h1", "Hello world", { class: "bold" }) | |
puts tag("h1", "Hello world", class: "bold") | |
puts tag("a", "Le Wagon", { href: "http://lewagon.org", class: "btn" }) | |
puts tag("a", "Le Wagon", href: "http://lewagon.org", class: "btn") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment