Created
January 18, 2019 11:15
-
-
Save rodloboz/c9d609e4d38576a2ee731dbe0279f409 to your computer and use it in GitHub Desktop.
Ruby Hashes - Batch 224
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
| "Hello world" # String | |
| 1 # Integer | |
| 3.14 # Float | |
| true # Boolean | |
| nil # Nil | |
| [1, 3, 6, "Hello"] # Array | |
| # boxes/container to store stuff/data | |
| first_name = "rui" | |
| last_name = "Freitas" | |
| # Contatenation: Adding two or more strings together | |
| # puts first_name + ' ' + last_name | |
| # Interpolation: Injecting ruby expression into strings | |
| #capitalize = false | |
| # puts "#{capitalize ? first_name.capitalize : first_name} #{last_name}" | |
| # Recipe / Set of intructions | |
| def full_name(first_name, last_name) # parameters | |
| "#{first_name.capitalize} #{last_name.capitalize}" | |
| end | |
| # You are preparing the meal... | |
| # the arguments in this case are defined line 10 and 11 | |
| # "rui" "Freitas" => arguments | |
| result = full_name(first_name, last_name) | |
| puts result | |
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
| # 0. 1. 2. 3. | |
| students = [ "Peter", "Mary", "George", "Emma" ] | |
| # 0. 1. 2. 3. | |
| student_ages = [ 24 , 25 , 22 , 20 ] | |
| # element. index | |
| # students << "John" | |
| students.each_with_index do |student, index| | |
| name = student | |
| age = student_ages[index] | |
| puts "#{name} is #{age} years old." | |
| end | |
| # students["John"] | |
| # Hashes: Dictionary-like structure | |
| # key/value pairs | |
| students = { | |
| "Peter" => 24, | |
| "Mary" => 25, | |
| "George" => 22, | |
| "Emma" => 20 | |
| } | |
| p students.class | |
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 = { | |
| "country" => "Portugal", | |
| "population" => 504718 | |
| } | |
| p lisbon.class | |
| puts lisbon.length | |
| # students[3] Arrays work with indexes | |
| # Hashes work with keys | |
| # Array CRUD | |
| # C: students = ['Peter', 'Mary'] | |
| # R: students[1] | |
| # U: students[1] = 'Susan' | |
| # D: students.delete_at(0) / students.delete('Peter') | |
| # Hash CRUD | |
| # Creating | |
| # lisbon = { | |
| # "country" => "Portugal", | |
| # "population" => 504718 | |
| # } | |
| # Reading | |
| puts lisbon["population"] | |
| # Updating | |
| lisbon["population"] = 604718 | |
| p lisbon | |
| # Deleting | |
| puts lisbon.delete("population") | |
| p lisbon | |
| # Adding more entries | |
| # students << "John" | |
| lisbon["monuments"] = ["Torre de Belém", "Jerónimos"] | |
| p lisbon | |
| lisbon["monuments"] << "Castelo de S. Jorge" | |
| 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
| lisbon = { | |
| "country" => "Portugal", | |
| "population" => 504718 | |
| } | |
| lisbon.each do |key, value| | |
| puts "The value of #{key} is #{value}" | |
| end | |
| london = {} | |
| p london.class | |
| p london.length | |
| puts lisbon.empty? | |
| puts london.empty? | |
| puts "============KEYS" | |
| puts lisbon.key?("country") | |
| puts london.key?("country") | |
| p lisbon.keys | |
| puts "===========VALUES" | |
| p 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
| students = { | |
| "Peter" => 24, | |
| "Mary" => 25, | |
| "George" => 22, | |
| "Emma" => 20 | |
| } | |
| p students | |
| puts "After one year..." | |
| # returns an array | |
| mapped_students = students.map do |name, age| | |
| "#{name} is now #{age + 1} years old" | |
| end | |
| p mapped_students | |
| aged_students = students.each do |key, age| | |
| # manually updating each key/value pair | |
| students[key] = age + 1 | |
| end | |
| p aged_students | |
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" => 24, | |
| "Mary" => 25, | |
| "George" => 22, | |
| "Emma" => 20 | |
| } | |
| count_22 = students.count do |_key, age| | |
| age <= 22 | |
| end | |
| puts count_22 | |
| students_with_4_letter_names = students.count do |name, _value| | |
| name.length == 4 | |
| end | |
| p students_with_4_letter_names |
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
| # paris = { | |
| # "country" => "France", | |
| # "population" => 2211000 | |
| # } | |
| # london = { | |
| # "country" => "England", | |
| # "population" => 8308000 | |
| # } | |
| paris = { | |
| country: "France", | |
| population: 2211000 | |
| } | |
| puts paris[: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
| def tag(tag_name, attributes = {}) | |
| attr_str = "" | |
| attributes.each do |key, value| | |
| attr_str += " #{key}=\"#{value}\"" | |
| end | |
| content = yield if block_given? | |
| "<#{tag_name}#{attr_str}>#{content}</#{tag_name}>" | |
| end | |
| attributes = { | |
| class: "container red button", | |
| id: "my-card", | |
| style: "color: red" | |
| } | |
| puts tag("div", attributes) { | |
| "Hello" | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment