Created
January 16, 2020 23:54
-
-
Save rodloboz/d842b3be8f01a07ad26a24c2e473c6d3 to your computer and use it in GitHub Desktop.
Ruby 04 - Hash and Symbols
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
# Built-int Objects | |
34 # Integer | |
"Hello world!" # String | |
3.14 # Float | |
true # Boolean | |
(1..10) # Range | |
[1, 2, "hello", ["a"]] # Array | |
nil # Nil | |
# box/container where stuff/information/data | |
# can be stored | |
first_name = "rui" | |
last_name = "freitas" | |
# this is a recipe | |
# or set of instructions | |
# (ruby will put this in memory for future user) | |
# parameters -> ingredients | |
def full_name(first_name, last_name) | |
# ruby has implicit returns | |
# (last line will be returned) | |
#first_name.capitalize + " " + last_name.capitalize # <= return | |
"#{first_name.capitalize} #{last_name.capitalize}" | |
end | |
# here I am preparing the meal/recipe | |
# using the in-memory instructions | |
# puts full_name("beatrix", "kiddo") | |
puts full_name(first_name, last_name) |
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
# From Arrays to Hashes | |
# 0 1 2 3 | |
students = [ "Peter", "Mary", "George", "Emma" ] | |
student_ages = [ 24 , 25 , 22 , 20 ] | |
# "Peter is 24 years old" | |
# "Mary is 25 years old" | |
# ... | |
# will iterate from 0 to 3 | |
# for index in (0...students.size) | |
# name = students[index] | |
# age = student_ages[index] | |
# puts "#{name} is #{age} years old" | |
# end | |
# students << "John" | |
# each_with_index : Iterator | |
students.each_with_index do |student, index| | |
age = student_ages[index] | |
puts "#{student} is #{age} years old" | |
end | |
students["Peter"] | |
students_age = { | |
"Peter" => 24, | |
"Mary" => 25, | |
"George" => 22, | |
"Emma" => 20 | |
} |
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
# Hashes | |
# { "key" => "value" } | |
melbourne = { | |
"country" => "Australia", | |
"population" => 4936000, | |
} | |
puts melbourne.class | |
puts melbourne.size | |
p melbourne | |
# CRUD | |
# Arrays | |
# C: students = ["Peter", "Mary"] | |
# R: students[1] | |
# U: students << "Susan" | |
# D: students.delete_at(0) | |
# Hashes CRUD | |
# Create | |
{ "key" => "value" } | |
# Read | |
population = melbourne["population"] | |
puts "Melbourne's population is #{population}" | |
# Update | |
melbourne["suburbs"] = ["Richmond", "South Yarra"] | |
# melbourne["population"] = melbourne["population"] + 1 | |
melbourne["population"] += 1 | |
p melbourne | |
suburbs = melbourne["suburbs"] | |
suburbs.each do |suburb| | |
puts suburb | |
end | |
# Delete | |
suburbs = melbourne["suburbs"] | |
puts suburbs.class | |
suburbs.delete("Richmond") | |
p melbourne | |
# melbourne.delete("suburbs") | |
# p melbourne | |
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
# Iteration | |
# Hashes are part of the Enumerable family | |
students = { | |
"Peter" => 24, | |
"Mary" => 25, | |
"George" => 22, | |
"Emma" => 20 | |
} | |
# hash#map => array (of the same size) | |
# mapped_students = students.map do |student| | |
# name = student[0] | |
# age = student[1] | |
# "#{name} is #{age} years old" | |
# end | |
# key , value | |
mapped_students = students.map do |name, age| | |
"#{name} is #{age} years old" | |
end | |
p mapped_students | |
# Each | |
aged_students = {} | |
students.each do |key, age| | |
aged_students[key] = age + 1 | |
end | |
p aged_students | |
p students | |
# Count => integer | |
count_22 = students.count do |_key, age| | |
age <= 22 | |
end | |
p count_22 | |
count_m = students.count do |name, _v| | |
name.start_with?("M") | |
end | |
p count_m | |
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
# Hash methods | |
melbourne = { | |
"country" => "Australia", | |
"population" => 4936000, | |
} | |
melbourne.each do |key, value| | |
puts "The value of #{key} is #{value}," | |
end | |
london = {} | |
# melbourne has 2 key/value pairs | |
puts melbourne.size | |
puts london.size | |
# if london.size == 0 | |
# puts "The hash is empty" | |
# else | |
# puts "The hash is not empty" | |
# end | |
if london.empty? | |
puts "The hash is empty" | |
else | |
puts "The hash is not empty" | |
end | |
puts "============= KEYS" | |
# keys => array of keys | |
p melbourne.keys | |
p melbourne.key?("suburbs") | |
p melbourne.key?("country") | |
puts "============= VALUES" | |
# .values => array of values | |
p melbourne.values | |
p melbourne.value?("abc") | |
p melbourne.value?("Australia") | |
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
# Symbols as keywords | |
# melbourne = { | |
# "country" => "Australia", | |
# "population" => 4936000, | |
# } | |
melbourne = { | |
country: "Australia", | |
population: 4936000 | |
} | |
puts melbourne[:country] | |
puts melbourne["country"] |
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
# => <h1>Hello world</h1> | |
# => <h1 class='bold'>Hello world</h1> | |
# => <a href='http://lewagon.org' class='btn'>Le Wagon</a> | |
def tag(tag_name, attributes = {}) | |
attr_html = "" | |
attributes.each do |key, value| | |
attr_html << "#{key}=\"#{value}\" " | |
end | |
"<#{tag_name} #{attr_html.strip}>#{yield}</#{tag_name}>" | |
"<#{tag_name} #{attr_html}>#{yield}</#{tag_name}>" | |
end | |
attributes = { | |
class: "bold", | |
id: "heading", | |
style: "color: red; font-size: 14px" | |
} | |
html = tag("h1", attributes) do | |
"Hello world" | |
end | |
puts html |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment