Skip to content

Instantly share code, notes, and snippets.

@elcortez
Created April 11, 2019 08:44
Show Gist options
  • Select an option

  • Save elcortez/42d9d35e641d1be0da4bb25ffbad94c4 to your computer and use it in GitHub Desktop.

Select an option

Save elcortez/42d9d35e641d1be0da4bb25ffbad94c4 to your computer and use it in GitHub Desktop.
musicians = [
'Jimmy Page',
'Robert Plant',
'John Paul Jones',
'John Bonham'
]
# SELECT
begins_with_r = musicians.select { |musician| musician.start_with?("R") }
# SELECT
begins_with_r = musicians.select do |musician|
musician.start_with?("R")
end
# EACH
begins_with_r = []
musicians.each do |musician|
begins_with_r << musician if musician.start_with?("R")
end
# COUNT
begins_with_r_count = musicians.count do |musician|
musician.start_with?("R")
end
# EACH
begins_with_r_count = 0
musicians.each do |musician|
begins_with_r += 1 if musician.start_with?("R")
end
# MAP
first_names = musicians.map do |musician|
musician.split(' ').first
end
# MAP
upcased = musicians.map do |musician|
musician.upcase
end
# EACH
upcased = []
musicians.each do |musician|
upcased << musician.upcase
end
# EACH 2
musicians.each do |musician|
musician.upcase!
end
# EACH WITH INDEX
musicians.each_with_index do |musician, index|
musicians[index] = musicians[index].upcase!
end
# EACH WITH INDEX
# musicians.each_with_index do |musician, index|
# p "#{index} - #{musician}"
# end
# EACH
# index = 0
# musicians.each do |musician|
# index += 1
# p "#{index} - #{musician}"
# end
# # FOR LOOP
# index = 0
# for musician in musicians
# index += 1
# p "#{index} - #{musician}"
# end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment