Created
January 15, 2020 23:57
-
-
Save rodloboz/1baa256f982f3bbb66aa710b82e28e45 to your computer and use it in GitHub Desktop.
Ruby 03 - 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
# Ranges | |
range1 = (1..5) | |
p range1.to_a | |
range2 = (1...5) | |
p range2.to_a | |
# Arrays | |
musicians = [ | |
"j dilla", | |
"nas", | |
"jimi hendrix", | |
"beyonce" | |
] | |
# Alternative shorthand syntax | |
# musicians = %w[ | |
# j\dilla | |
# nas | |
# jimi\ hendrix | |
# beyonce | |
# ] | |
p musicians | |
# CRUD | |
# Create / Read / Update / Delete | |
# Create | |
an_array = [] | |
# an_array = Array.new | |
p an_array.class | |
# Read | |
puts musicians[2] | |
# Update | |
# Appending | |
musicians << "ac/dc" | |
p musicians | |
# Prepending | |
musicians.unshift("nicolai") | |
# last element | |
# musicians.last - can only be used for reading | |
# p musicians[-1] | |
# p musicians [musicians.length - 1] | |
musicians[musicians.length - 1] = "jay z" | |
p musicians | |
# Inserting | |
musicians.insert(1, "silverchair") | |
p musicians | |
# Delete | |
# musicians.delete_at(-1) # deleting by index | |
# p musicians | |
musicians.delete("beyonce") # deletes all instances/elements | |
p musicians | |
# Iterating over the elements of an Array | |
#. element in. array | |
for musician in musicians | |
# names = musician.split | |
# first_name = names[0].capitalize unless names[0].nil? | |
# last_name = names[1].capitalize unless names[1].nil? | |
# full_name = "#{first_name} #{last_name}".strip | |
puts "Hey, #{musician}!" | |
end | |
p musicians | |
# 1 - Jimi Hendrix | |
# Iterating over indexes | |
for index in (0...musicians.size) | |
puts "#{index + 1} - #{musicians[index]}" | |
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 = [ | |
"j dilla", | |
"nas", | |
"jimi hendrix", | |
"beyonce" | |
] | |
# each => returns the original array | |
# "Hey, jimi hendrix!" | |
# element | |
# array.each { |el| # do somth } | |
musicians.each do |musician| | |
puts "Hey, #{musician}!" | |
end | |
p musicians | |
# each_with_index | |
musicians.each_with_index do |musician, index| | |
puts "#{index + 1} - #{musician}" | |
end | |
# upcased_musicians | |
upcased_musicians = [] | |
musicians.each do |musician| | |
upcased_musicians << musician.upcase | |
end | |
p musicians | |
p upcased_musicians | |
# count musicians that start with j | |
results = musicians.count do |musician| | |
# count the element if condition is true | |
musician.start_with?("j") | |
end | |
# musicians_with_j = [] | |
musicians_with_j = [] | |
musicians.each do |musician| | |
musicians_with_j << musician if musician.start_with?("j") | |
end | |
p musicians_with_j | |
# map => new (transformed) array | |
result = musicians.map do |musician| | |
musician.upcase | |
end | |
p musicians | |
p result | |
# select => a new array (maybe not of the same length) | |
# musicians_with_j | |
musicians_with_j = musicians.select do |musician| | |
condition1 = musician.start_with?("j") | |
condition2 = musician.length == 7 | |
condition1 && condition2 | |
# musician.start_with?("j") | |
end | |
p musicians_with_j | |
# find => element (return the first matched element) | |
p musicians.find { |musician| musician.start_with?("j") } | |
# arr.inject(10) { |total, el| total += el } | |
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
# Yield | |
def block_example(string) | |
puts "Running the block (line 4)" | |
# yield # run the block that is passed into the method call | |
yield if block_given? # only run block if it exists | |
puts "#{string.upcase} (line 8)" | |
end | |
block_example("hello") do | |
puts "Hello from inside the block (line 13)" | |
end | |
block_example("bye") | |
def upcase_string(string) | |
string.upcase | |
end | |
def my_own_each(array) | |
for element in array | |
yield(element) | |
end | |
end | |
names = ['john', 'mary'] | |
my_own_each(names) do |name| | |
puts upcase_string(name) | |
end | |
my_own_each(names) do |name| | |
puts name.capitalize | |
end | |
my_own_each(names) do |name| | |
puts name[0] | |
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 big_sort | |
puts "Running big sort..." | |
(0..10000000).sort | |
end | |
def small_sort | |
puts "Running small sort..." | |
(0..100000).sort | |
end | |
def timer | |
puts "Starting the timer" | |
start_time = Time.now | |
# big_sort | |
# small_sort | |
yield | |
puts "End the timer" | |
end_time = Time.now | |
puts end_time - start_time | |
end | |
timer do | |
big_sort | |
end | |
timer do | |
small_sort | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment