Last active
February 8, 2023 00:17
-
-
Save caioertai/050b6da8d94e97489fa0daf7cac7141e to your computer and use it in GitHub Desktop.
Ruby Day 3 Livecode - Iterators and Blocks
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
musicians = ['David Gilmour', 'Roger Waters', 'Richard Wright', 'Nick Mason'] | |
# 0 1 2 3 | |
puts musicians.length | |
# Array CRUD | |
# Create | |
# musicians.push("Steven Wilson") | |
musicians << "Steven Wilson" | |
p musicians | |
# Read | |
p musicians[2] | |
# Update | |
musicians[2] = "James LaBrie" | |
p musicians[2] | |
# Delete | |
musicians.delete_at(1) | |
musicians.delete("Nick Mason") | |
p musicians |
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 | |
# .. ... | |
# .. 2 dots | |
array_1_to_10 = (1..10).to_a | |
p array_1_to_10 | |
# ... 3 dots | |
array_1_to_9 (1...10).to_a | |
p array_1_to_9 |
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
musicians = ['David Gilmour', 'Roger Waters', 'Richard Wright', 'Nick Mason'] | |
# Let's loop with the array | |
for musician in musicians | |
p musician | |
end | |
# Let's loop with just the indexes | |
for index in 0...(musicians.length) | |
p musicians[index] | |
end | |
# Now forget about for and focus on iterators! =) |
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
musicians = [ | |
'David Gilmour', | |
'Roger Waters', | |
'Richard Wright', | |
'Nick Mason' | |
] | |
# Let's say hi to them | |
musicians.each do |musician| | |
p "Hi, #{musician}!" | |
end | |
# Let's list with numbers | |
musicians.each_with_index do |musician, index| | |
p "#{index + 1}. #{musician}" | |
end | |
# Let's list with numbers (but without each_with_index) | |
# PS.: this is just for exercise. Prefer the above. | |
counter = 1 | |
musicians.each do |musician| | |
p "#{counter}. #{musician}" | |
counter += 1 | |
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
musicians = [ | |
'David Gilmour', | |
'Roger Waters', | |
'Richard Wright', | |
'Nick Mason' | |
] | |
# Let's map this to a new array: | |
# - upcased array | |
upcased_musicians = musicians.map do |musician| | |
musician.upcase | |
end | |
p musicians | |
p upcased_musicians | |
# - first names array | |
first_names_array = musicians.map do |musician| | |
musician.split.first | |
end | |
p first_names_array |
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
musicians = [ | |
'David Gilmour', | |
'Roger Waters', | |
'Richard Wright', | |
'Nick Mason' | |
] | |
# Let's count the musicians starting with "R" | |
starting_with_r_count = musicians.count do |musician| | |
musician.start_with?("R") | |
end | |
p starting_with_r_count | |
# Now let's do it with just #each | |
# PS.: The above is ofc still preferred | |
counter = 0 | |
musicians.each do |musician| | |
if musician.start_with?("R") | |
counter += 1 | |
end | |
end | |
p counter |
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
musicians = [ | |
'David Gilmour', | |
'Roger Waters', | |
'Richard Wright', | |
'Nick Mason' | |
] | |
# Select (filter) musicians starting with "R" | |
musicians_starting_with_r = musicians.select do |musician| | |
musician.start_with?("R") | |
end | |
p musicians_starting_with_r | |
# Reject musicians starting with "R" | |
musicians_not_starting_with_r = musicians.reject do |musician| | |
musician.start_with?("R") | |
end | |
p musicians_not_starting_with_r |
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 | |
def timer | |
start_time = Time.now | |
yield | |
end_time = Time.now | |
puts "The task took #{end_time - start_time} seconds" | |
end | |
# Calling our timer method | |
timer do | |
puts "Starting task 1..." | |
sleep 2 | |
puts "Finished task 1!" | |
end | |
# Calling our timer method (with a different block of code) | |
timer do | |
puts "Starting task 2..." | |
sleep 1 | |
puts "Finished task 2!" | |
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 stylize_name(first_name, last_name) | |
full_name = "#{first_name.capitalize} #{last_name.upcase}" | |
yield(full_name) | |
end | |
# We want to customize this stylize_name message with a block. | |
message = stylize_name("ringo", "starr") do |name| | |
"Hey, #{name}! Learn to play!" | |
end | |
puts message |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment