Skip to content

Instantly share code, notes, and snippets.

@alex-benoit
Created July 14, 2017 09:36
Show Gist options
  • Save alex-benoit/ea912b5ff5e4742d3b43b9394054d879 to your computer and use it in GitHub Desktop.
Save alex-benoit/ea912b5ff5e4742d3b43b9394054d879 to your computer and use it in GitHub Desktop.
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
cities = ["london", "paris", "new york"]
# index 0 1 2
cities[1] # => "paris"
Paris 2211000 Tour Eiffel
London 8308000 Big Ben
require 'csv'
CSV.foreach('cities.csv') do |row|
puts "The city of #{row[0]} has #{row[1]} inhabitants"
end
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
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
students = {
"amy" => 27,
"bob" => 25,
"james" => 25
}
count_25 = students.count do |student, age|
age == 25
end
p count_25
paris = {
"country" => "France",
"population" => 2211000,
}
puts paris.has_key?("monument")
p paris.length
def full_name(attributes)
name = "#{attributes[:first].capitalize} #{attributes[:last].capitalize}"
return name
end
puts full_name(last: 'benoit', first: 'alex')
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"]}"
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
paris = {
"country" => "France",
"population" => 2211000
}
puts paris["country"]
paris = {
:country => "France",
:population => 2211000
}
paris = {
country: "France",
population: 2211000
}
puts paris[:country]
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