Created
April 14, 2022 16:08
-
-
Save caioertai/f79bb6510f435dbf9e04a1dc98d29703 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
| # Declare an array literal | |
| # 0 1 2 3 | |
| characters = ["Hank", "Sheila", "Bob"] # "Uni" | |
| # Array CRUD | |
| # Create (add elements) | |
| characters << "Uni" | |
| # Read | |
| characters[1] | |
| # Update | |
| characters[1] = "Diana" | |
| # Delete | |
| # index | |
| characters.delete_at(2) |
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
| # Range | |
| # .. | |
| p 3..10 | |
| p (3..10).to_a | |
| # ... | |
| p 3...10 | |
| p (3...10).to_a |
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
| # Let's loop with the array | |
| # 0 1 2 | |
| # -3 -2 -1 | |
| characters = ["Hank", "Sheila", "Bob"] | |
| # for when given an array | |
| # el array | |
| for character in characters | |
| puts "- #{character}" | |
| end | |
| # Let's loop with just the indexes | |
| # 0 1 2 | |
| for index in (0...characters.count) | |
| # 0 -> characters[0] -> "Hank" | |
| # 1 -> characters[1] -> "Sheila" | |
| puts "- #{characters[index]}" | |
| end | |
| # Prefer this | |
| characters.each do |character| | |
| puts "--- #{character}" | |
| 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
| # 0 1 2 3 | |
| characters = ["Hank", "Sheila", "Bob", "Erik"] | |
| # Let's print hi to all of them | |
| # Hi, Hank! ... etc | |
| # characters.each do |name| | |
| # puts "Hi, #{name}!" | |
| # end | |
| # Print characters as a numbered list | |
| index = 0 | |
| characters.each do |character| | |
| puts "#{index + 1}. #{character}" | |
| index += 1 | |
| end | |
| characters.each_with_index do |character, index| | |
| puts "#{index + 1}. #{character}" | |
| 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
| characters = [ | |
| "Hank", | |
| "Sheila", | |
| "Bob", | |
| "Erik" | |
| ] | |
| # Let's change our array to a new array: | |
| # - upcased names | |
| upcased_characters = characters.map do |character| | |
| character.upcase | |
| end | |
| p upcased_characters | |
| first_letters = characters.map do |character| | |
| character[0] | |
| end | |
| p first_letters |
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
| characters = ["Diana", "Bob", "Dungeon Master", "Erik"] | |
| # Let's count the names with the letter D | |
| d_count = characters.count do |character| | |
| character.include?("D") | |
| end | |
| p d_count | |
| long_names_count = characters.count do |character| | |
| character.length > 3 | |
| end | |
| p long_names_count |
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
| characters = ["Hank", "Dungeon Master", "Bob", "Diana"] | |
| # Select names with less than 5 letters | |
| short_names = characters.select do |character| | |
| character.length < 5 | |
| end | |
| p short_names | |
| # Reject (filter out) names with less than 5 letters | |
| long_names = characters.reject do |character| | |
| character.length < 5 | |
| end | |
| p long_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
| # What_is_this? | |
| method_name do |param| | |
| "code goes here" | |
| end | |
| # And_this? | |
| method_name { |param| "code goes here" } |
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
| # Let's write a benchmark tool using yield | |
| def timer | |
| start_time = Time.now | |
| # Do stuff here | |
| yield | |
| end_time = Time.now | |
| return end_time - start_time | |
| end | |
| time1 = timer do | |
| sleep 1 | |
| end | |
| p time1 | |
| time2 = timer do | |
| Array.new(100000) { rand }.sort | |
| end | |
| p time2 |
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 full_name_with_message(first_name, last_name) | |
| sanitized_first_name = first_name.capitalize | |
| sanitized_last_name = last_name.upcase | |
| full_name = "#{sanitized_last_name} #{sanitized_first_name}" | |
| return yield(full_name) | |
| end | |
| greeting1 = full_name_with_message("ringo", "starr") do |name| | |
| "Olá, #{name}. Como é que tu estás?" | |
| end | |
| puts greeting1 | |
| greeting2 = full_name_with_message("john", "lennon") do |name| | |
| "Hey, how are you, #{name}?" | |
| end | |
| puts greeting2 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment