Skip to content

Instantly share code, notes, and snippets.

@marcoranieri
Created February 21, 2020 10:36
Show Gist options
  • Save marcoranieri/db21a64b7fea79f12eb9a498a038b606 to your computer and use it in GitHub Desktop.
Save marcoranieri/db21a64b7fea79f12eb9a498a038b606 to your computer and use it in GitHub Desktop.
Hash & Symbols
students = [ "Peter", "Mary", "George", "Emma" ]
student_ages = [ 24 , 25 , 22 , 20 ]
# "Peter is 24 yo!"
# "Mary is 25 yo!"
# ...
students.each_with_index do |student, index|
puts "#{student} is #{student_ages[index]}"
end
student_ages = {
"Peter" => 24,
"Mary" => 25,
"George" => 22
}
puts student_ages["Peter"]`
require 'json'
require 'open-uri'
puts "Give me a username:"
nickname = gets.chomp
serialized_json = open("https://api.github.com/users/#{nickname}").read
user_hash = JSON.parse(serialized_json)
puts "#{user_hash["name"]} has #{user_hash["public_repos"]} public repos!"
students = [ "Peter", "Mary", "George", "Emma" ]
# paris = {
# "population" => 2211000,
# "country" => "France",
# }
# CRUD
# Array
# C. students.push("mario") / <<
# R. students[1]
# U. students[2] = "Luca"
# D. students.delete("Mary") / .delete_at(1)
# Hash
# C./U. paris["monument"] = "Tour Eiffel"
# R. paris["population"]
# D. paris.delete("population")
paris = [
["population", 2211000],
["country", "France"]
]
puts paris[1][1]
puts paris["country"]
puts paris.key?("country")
puts paris.key?("house")
paris.each do |key, value|
puts "#{key} is #{value}!"
end
# paris.each do |object|
# p object
# end
paris = {
"country" => "France",
"population" => 2211000
}
paris = {
:country => "France",
:population => 2211000
}
paris = {
country: "France",
population: 2211000
}
# Text data => String
"Sebastien Saunier"
"seb@lewagon.org"
"ruby on Rails"
"Paris"
# Text identifiers => Symbol
:fullname
:email
:skill
:city
tag("h1", "Hello world", attr = {})
<h1 class="container" id="title" style="color:red;">
Hello world
</h1>
def tag(tag_name, content = "", attr = {})
# {} -> [] .map
attributes = attr.map { |key, value| "#{key.to_s}: #{value}" }.join
return "<#{tag_name} #{attributes.join}>#{content}</#{tag_name}>"
end
tag("h1", "Hello world")
tag("h1", "Hello world", class: "container", id: "title", style: "color:red;")
<h1 class="container" id="title" style="color:red;">
Hello world
</h1>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment