Created
January 17, 2019 10:46
-
-
Save rodloboz/be2f777cecea33942a5c6d1dba52510e to your computer and use it in GitHub Desktop.
Ruby 03-Array-Blocks Lecture
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 | |
range_1 = (1..10) # includes the last | |
p range_1 | |
range_2 = (1...10) # does not include the last | |
p range_2.to_a | |
# Arrays | |
movies_stars = [ | |
"Nicole Kidman", | |
"Kurt Russel", | |
"Johnny Depp", | |
"Jennifer Lawrence" | |
] | |
p movies_stars | |
# Alternative array of strings syntax | |
# %w[nicole\ kidman kurt\ russel] | |
puts movies_stars.length | |
# alternative: #size #count | |
# movies_stars.push("Leonardo DiCaprio") | |
movies_stars << "Leonardo DiCaprio" | |
p movies_stars | |
# Reading | |
puts movies_stars[3] | |
# Updating | |
movies_stars[3] = "Drew Barrymore" | |
p movies_stars | |
puts movies_stars.last | |
# movies_stars[-1] | |
# movies_stars[movies_stars.length - 1] | |
# insert at a specific index | |
movies_stars.insert(3, "Brad Pitt") | |
p movies_stars | |
# iterate over indexes with for | |
# for i in Array/Range | |
# # Do something | |
# end | |
for index in 0...movies_stars.length | |
movie_star = movies_stars[index] | |
puts "#{index + 1} - Star: #{movie_star}" | |
end | |
for movie_star in movies_stars | |
puts "Watching a movie with #{movie_star}!" | |
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
# Arrays | |
movies_stars = [ | |
"nicole kidman", | |
"kurt russel", | |
"johnny depp", | |
"jennifer lawrence" | |
] | |
# capitalized_stars = [] | |
# movies_stars.each do |movie_star| | |
# # names = movie_star.split | |
# # first_name = names.first | |
# # last_name = names.last | |
# first_name, last_name = movie_star.split | |
# capitalized_stars << "#{first_name.capitalize} #{last_name.capitalize}" | |
# end | |
# #map => returns a transformed array of the | |
# same size as the original array | |
capitalized_stars = movies_stars.map do |movie_star| | |
# names = movie_star.split | |
# first_name = names.first | |
# last_name = names.last | |
first_name, last_name = movie_star.split | |
"#{first_name.capitalize} #{last_name.capitalize}" | |
end | |
p capitalized_stars | |
puts "List of Movie Stars:" | |
# element , index | |
movies_stars.each_with_index do |movie_star, index| | |
puts "#{index + 1}) #{movie_star}" | |
end | |
# movies_stars.each { |movie_star| puts "Hello, #{movie_star}!" } | |
# count | |
j_movie_stars = movies_stars.count do |movie_star| | |
movie_star.downcase.start_with?("j") | |
end | |
p j_movie_stars | |
# count | |
k_movie_stars = movies_stars.count do |movie_star| | |
last_name = movie_star.split.last | |
last_name.downcase.start_with?("k") | |
end | |
p k_movie_stars | |
# counter = 0 | |
# movies_stars.each do |movie_star| | |
# condition = movie_star.downcase.start_with?("j") | |
# counter += 1 if condition | |
# end | |
# p counter | |
# find => returns the first element | |
# that matches the condition | |
# select => returns all the elements | |
# that match the condition | |
movie_stars_with_j_first_name = movies_stars.select do |movie_star| | |
movie_star.downcase.start_with?("j") | |
end | |
p movie_stars_with_j_first_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
def big_sort | |
(0..10000000).sort | |
end | |
def small_sort | |
(0..1000000).sort | |
end | |
def timer | |
start_time = Time.now | |
# call big_sort | |
# big_sort | |
yield if block_given?# execute the block | |
end_time = Time.now | |
puts end_time - start_time | |
end | |
timer do | |
(0..10000000).sort | |
end | |
timer { small_sort } | |
def greet(name) | |
capitalized_name = name.capitalize | |
puts "[DEBUG] Starting the block" | |
yield(capitalized_name) # execute block | |
puts "[DEBUG] Block has been completed!" | |
end | |
greet("beatrix") do |name| | |
puts "Hello, #{name}!" | |
end | |
greet("michael") do |name| | |
puts "Good morning, #{name}!" | |
puts "How are you?" | |
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 my_each(array) | |
for element in array | |
# Do something | |
yield(element) | |
end | |
array | |
end | |
names = ["beatrix", "michael"] | |
# yielded element | |
my_each(names) do |name| | |
puts "Hello, #{name.capitalize}" | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment