Skip to content

Instantly share code, notes, and snippets.

@rodloboz
Created January 18, 2019 08:51
Show Gist options
  • Save rodloboz/24360b2065872922772216bc2d7580f5 to your computer and use it in GitHub Desktop.
Save rodloboz/24360b2065872922772216bc2d7580f5 to your computer and use it in GitHub Desktop.
prep
[1, 2, 3, "hello"] # Array
"hello" # String
5 # Integer
1.356 # Float
(1..10) # Range
true # boolean
nil # Nil
# box/container to store stuff/data
# so it can be used again
first_name = "rui"
last_name = "freitas"
# this is a recipe
# or a set of instructions
#. string, string
def full_name(first_name, last_name)
# concatenation
# first_name.capitalize + " " + last_name.capitalize
# interpolation
"#{first_name.capitalize} #{last_name.capitalize}"
end
# here i am preparin the meal
#. "rui" "freitas"
puts full_name(first_name, last_name)
# 0. 1. 2. 3
students = [ "Peter", "Mary", "George", "Emma" ]
# 0. 1. 2. 3
student_ages = [ 24 , 25 , 22 , 20 ]
# length is 4
"Peter is 24 years of age"
# for i in (0...students.length)
# name = students[i]
# age = student_ages[i]
# puts "#{name} is #{age} years of age"
# end
students.each_with_index do |student, index|
age = student_ages[index]
name = student
puts "#{name} is #{age} years of age"
end
# students << "John"
students = {
"Peter" => 24,
"Mary" => 25,
"George" => 22,
"Emma" => 20
}
# students["Peter"]
lisbon = {
"country" => "Portugal",
"population" => 504718
}
puts lisbon.class
puts lisbon.size
p lisbon
# CRUD
# Arrays
# students = ["Peter", "Mary"]
# C: students << "John"
# R: students[1]
# U: students[0] = "Susan"
# D: students.delete_at(2)
# hashes CRUD
# Create
lisbon["monuments"] = ["Torre de Belém", "Castelo de S. Jorge"]
p lisbon
# Reading
puts lisbon["monuments"]
# Updating
lisbon["population"] = 600000
p lisbon
# the value is an array
# so i can use array methods
lisbon["monuments"] << "Ponte 25 de Abril"
p lisbon
lisbon["monuments"][1] = "Estádio da Luz"
p lisbon
# Delete
lisbon.delete("monuments")
p lisbon
lisbon = {
"country" => "Portugal",
"population" => 504718
}
lisbon.each do |key, value|
puts "The value of #{key} is #{value}"
end
london = {}
# lisbon has two key/value pairs
# puts lisbon.size == 0
puts lisbon.empty?
puts london.empty?
puts "======= KEYS"
puts lisbon.key?("country")
puts lisbon.key?("monuments")
# return an array with keys
p lisbon.keys
puts "===== VALUES"
# return an array with values
p lisbon.values
students = {
"Peter" => 24,
"Mary" => 25,
"George" => 22,
"Emma" => 20
}
mapped_students = students.map do |name, age|
"#{name} is #{age} years old."
end
# Array
p mapped_students.class
# Each
aged_students = students.each do |key, value|
students[key] = value + 1
end
p aged_students
students = {
"Peter" => 24,
"Mary" => 25,
"George" => 22,
"Emma" => 20
}
# => returns an integer
count_22 = students.count do |_key, age|
age <= 22
end
four_letter = students.count do |name, _age|
name.length == 4
end
p count_22
p four_letter
# keys as strings
paris = {
"country" => "France",
"population" => 2211000
}
# keys as symbols
london = {
country: "England",
population: 8308000
}
puts london[:country]
require "byebug"
#. opening content closing
# <div>Hello</div>
# <a href="www.lewagon.com">Click me!</a>
# <h1 class="title" id="main_heading">Welcome!</h1>
def tag(tag_name, content, attributes = {})
attr_string = ""
attributes.each do |name, value|
attr_string += " #{name}=\"#{value}\""
end
"<#{tag_name}#{attr_string}>#{content}</#{tag_name}>"
end
puts tag("div", "Hello")
attributes = {
href: "www.lewagon.com"
}
puts tag("a", "Click me!", attributes)
attributes = {
class: "title",
id: "main_heading",
}
puts tag("h1", "Welcome!", attributes)
require "csv"
file = "cities.csv"
cities = []
CSV.foreach(file) do |row|
city = {
name: row[0],
population: row[1],
monument: row[2]
}
cities << city
end
p cities
require "byebug"
require "json"
require "open-uri"
puts "What's your Github username?"
username = gets.chomp
url = "https://api.github.com/users/#{username}"
response = open(url).read
json = JSON.parse(response)
first_name = json["name"].split.first
public_repos = json["public_repos"]
puts "#{first_name} has #{public_repos} public repos!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment