Created
April 20, 2018 10:24
-
-
Save rodloboz/955b60d62cc450b0ede4f8ece80b3977 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
lisbon = { | |
# key => value | |
"country" => "Portugal", # string | |
"population" => 2821876 # integer | |
} | |
# puts Lisbon.class | |
# uninitialized constant Lisbon (NameError) | |
# the program does not know a variable/constant named Paris | |
# (because we have not defined one) | |
puts lisbon.class | |
puts lisbon.size | |
# shares some methods with array | |
# both are enumerators | |
# CRUD | |
# students = ["paul", "john"] | |
# C: students << "george" | |
# R: students[1] | |
# U: students[1] = "ringo" | |
# D: students.delete_at(2) -> () because delete_at is a method | |
# Hash | |
# Create / Update | |
lisbon["monument"] = "Belém Tower" | |
# key value | |
p lisbon | |
# Read | |
puts lisbon["population"] | |
# key | |
# Delete | |
lisbon.delete("population") | |
p lisbon |
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
students = { | |
"paul" => 24, | |
"mary" => 24, | |
"mark" => 25 | |
} | |
count_24 = students.count do |record| # grabs an array [key, value] | |
name = record[0] | |
age = record[1] | |
age == 24 | |
end | |
puts count_24 |
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
students = [ "Peter", "Mary", "George", "Emma" ] | |
student_ages = [ 24 , 25 , 22 , 20 ] | |
# 0 1 2 3 | |
students.each_with_index do |student, index| | |
age = student_ages[index] | |
puts "#{student} is at #{age} years old" | |
end | |
# Peter is 24 years old | |
# Mary is 25 years old | |
# [...] |
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
lisbon = { | |
# key => value | |
"country" => "Portugal", # string | |
"population" => 2821876, # integer | |
"neighbourhoods" => ["Bairro Alto", "Alfama", "Mouraria"] | |
} | |
# Q: Is there a 'monument' key in the 'lisbon' hash? | |
key_present = false | |
lisbon.each do |key, _value| | |
key_present = true if key == "monument" | |
end | |
puts key_present | |
puts lisbon.has_key?("population") | |
puts lisbon.has_value?("portugal") # => false 'portugal' != 'Portugal' | |
puts "====== Keys" | |
puts lisbon.keys | |
puts "====== Values" | |
puts lisbon.values |
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
lisbon = { | |
# key => value | |
"country" => "Portugal", # string | |
"population" => 2821876, # integer | |
"neighbourhoods" => ["Bairro Alto", "Alfama", "Mouraria"] | |
} | |
# hash iteration | |
lisbon.each do |key, value| | |
puts "The key is #{key} and the value is #{value}" | |
end | |
lisbon["neighbourhoods"].each do |neighbourhood| | |
puts neighbourhood | |
end | |
# array iteration | |
students = ["paul", "john"] | |
students.each do |student| | |
puts student | |
end | |
students.each_with_index do |student, index| | |
puts "The students at index #{index} is #{student}" | |
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
def tag(name, content, attrs = {}) | |
flat_attrs = attrs.map { |key, val| " #{key}='#{val}'" }.join | |
"<#{name}#{flat_attrs}>#{content}</#{name}>" | |
end | |
puts tag('div', 'hello', { | |
class: 'heading', | |
id: 'main-heading', | |
style: 'color: red;' | |
}) |
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
# variable assignment | |
first_name = "Rui" | |
# putting the string "Rui" inside the 'box'/'container' | |
last_name = "Freitas" | |
# putting the string "Freitas" inside another 'box'/'container' | |
# defining a method | |
# it's like a recipe/instructions for a meal | |
def full_name(first_name, last_name) # arguments are like the ingredients | |
"#{first_name} #{last_name}" | |
end | |
# now we are preparing/executing the meal | |
full_name(first_name, last_name) | |
# we can store the prepared meal/result for future use | |
my_name = full_name(first_name, last_name) | |
puts my_name | |
6 # Integer | |
3.14 # Float | |
"Hello, world!" # String | |
true # Boolean | |
['a', 'b', 'c', 'd'] # Array | |
cities = ["lisbon", "london"] | |
# 0 1 | |
puts cities[1] |
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
"country".object_id # => 70367016225180 | |
"country".object_id # => 70367024806360 | |
:country.object_id # => 2525468 | |
:country.object_id # => 2525468 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment