Created
July 14, 2017 09:36
-
-
Save alex-benoit/ea912b5ff5e4742d3b43b9394054d879 to your computer and use it in GitHub Desktop.
This file contains 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
cities = ["london", "paris", "new york", "london"] | |
# CRUD | |
# CREATE | |
cities << "madrid" | |
# READ | |
cities[2] | |
# UPDATE | |
cities[1] = "san francisco" | |
# DELETE | |
cities.delete_at(2) | |
cities.delete("london") | |
p cities |
This file contains 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
cities = ["london", "paris", "new york"] | |
# index 0 1 2 | |
cities[1] # => "paris" |
This file contains 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 | 2211000 | Tour Eiffel | |
---|---|---|---|
London | 8308000 | Big Ben |
This file contains 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
require 'csv' | |
CSV.foreach('cities.csv') do |row| | |
puts "The city of #{row[0]} has #{row[1]} inhabitants" | |
end |
This file contains 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, | |
} | |
# CRUD | |
# CREATE | |
paris["monument"] = "Eiffel Tower" | |
# READ | |
puts paris["population"] | |
# UPDATE | |
paris["population"] = 2311000 | |
# DELETE | |
paris.delete("population") | |
p paris |
This file contains 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
cities = ["paris", "london", "madrid"] | |
# cities.each do |city| | |
# puts city | |
# end | |
paris = { | |
"country" => "France", | |
"population" => 2211000, | |
} | |
paris.each do |key, value| | |
puts "the #{key} is #{value}" | |
end |
This file contains 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
students = { | |
"amy" => 27, | |
"bob" => 25, | |
"james" => 25 | |
} | |
count_25 = students.count do |student, age| | |
age == 25 | |
end | |
p count_25 |
This file contains 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, | |
} | |
puts paris.has_key?("monument") | |
p paris.length |
This file contains 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
def full_name(attributes) | |
name = "#{attributes[:first].capitalize} #{attributes[:last].capitalize}" | |
return name | |
end | |
puts full_name(last: 'benoit', first: 'alex') |
This file contains 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
require "json" | |
require 'open-uri' | |
puts "what is you gh username?" | |
user = gets.chomp | |
result = open("https://api.github.com/users/#{user}").read | |
user = JSON.parse(result) | |
puts "your name is: #{user["name"]}" |
This file contains 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
students = [ "Peter", "Mary", "George", "Emma" ] | |
student_ages = [ 24 , 25 , 22 , 20 ] | |
# index 0 1 2 3 | |
# Peter is 24 | |
# Mary is 25 | |
students.each_with_index do |student, index| | |
age = student_ages[index] | |
puts "The student #{student} is #{age}" | |
end | |
# 4.times do |i| | |
# age = student_ages[i] | |
# name = students[i] | |
# puts "The student #{name} is #{age}" | |
# end | |
This file contains 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 | |
} | |
puts paris["country"] | |
paris = { | |
:country => "France", | |
:population => 2211000 | |
} | |
paris = { | |
country: "France", | |
population: 2211000 | |
} | |
puts paris[:country] |
This file contains 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
def tag(tag_name, content, attributes = {}) | |
attrs = "" | |
attributes.each do |key, value| | |
attrs << " #{key}='#{value}'" | |
end | |
"<#{tag_name}#{attrs}>#{content}</#{tag_name}>" | |
end | |
puts tag("h1", "Some Title", id: "main-title", class: "bold") | |
puts tag("p", "Some Text") | |
# <h1 class='bold' id='main-title'>Some Title</h1> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment