Skip to content

Instantly share code, notes, and snippets.

@leahgarrett
Last active March 26, 2019 23:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save leahgarrett/574f297c789c38618915740c07d10ec5 to your computer and use it in GitHub Desktop.
Save leahgarrett/574f297c789c38618915740c07d10ec5 to your computer and use it in GitHub Desktop.
map in ruby

map

  • tranforming one collection into another collection
array = [1,2,3,4,5]
new_array = array.map do |number|
    number * 2
end
print new_array

Will display

[2, 4, 6, 8, 10]

For strings:

job_titles = ['developer', 'analyst', 'manager', 'designer', 'engineer']
puts "\nOriginal job titles"
print job_titles

capital_job_titles = job_titles.map do |job_title|
    job_title.capitalize
end
puts "\nCapitalized job titles"
print capital_job_titles

Will display

Original job titles
["developer", "analyst", "manager", "designer", "engineer"]
Capitalized job titles
["Developer", "Analyst", "Manager", "Designer", "Engineer"]

To transform an array using each we would need to build the new array.

prices = [5.50, 7.00, 6.00, 9.00, 4.50]
puts "\n\nOriginal prices"
print prices

puts "\nCalculate discounted prices using each loop"
discounted_prices = []
prices.each do |price|
  discounted_prices << price - price * 0.5
end
print discounted_prices

puts "\nCalculate discounted prices using map"
discounted_prices = prices.map do |price|
  price - price * 0.5
end
print discounted_prices

puts "\nCalculate discounted prices using single line map"
discounted_prices = prices.map { |price| price - price * 0.5 }
print discounted_prices

# Map can be very powerful when chained. 
string_prices = ['5.50', '7.00', '6.00', '9.00', '4.50']
puts "\nConvert string prices to integer and calculate price"
discounted_prices = string_prices.map { |value| value.to_i }.map { |price| price - price * 0.5 }
print discounted_prices

Change existing array using the bang operator

array = [1,2,3,4,5]
array.map! do |number|
    number * 2
end
print array

Will display

[2, 4, 6, 8, 10]
alphabet = ('a'..'z').to_a
print alphabet
# 1. write a map to create a capitalized version of the alphabet array
# 2. display the new array
numbers = (1..100).to_a
print numbers
# 3. write a map to create a new version of the numbers array that has all the values tripled
# 4. display the new array
# 5. write a map to create a new version of the numbers array that will show the numbers as percentages eg: 50 will become 0.5
# 6. display the new array
# job_titles = ['developer', 'analyst', 'manager', 'designer', 'engineer']
# puts "\nOriginal job titles"
# print job_titles
# capital_job_titles = job_titles.map do |job_title|
# job_title.capitalize
# end
# puts "\nCapitalized job titles"
# print capital_job_titles
# 7. Alter the above code to use the bang operator with map and remove the capital_job_titles variable
# 8. Alter the code so the map is on one line
# prices = [5.50, 7.00, 6.00, 9.00, 4.50]
# puts "\n\nOriginal prices"
# print prices
# puts "\nCalculate discounted prices using each loop"
# discounted_prices = []
# prices.each do |price|
# discounted_prices << price - price * 0.5
# end
# print discounted_prices
# puts "\nCalculate discounted prices using map"
# discounted_prices = prices.map do |price|
# price - price * 0.5
# end
# print discounted_prices
# puts "\nCalculate discounted prices using single line map"
# discounted_prices = prices.map { |price| price - price * 0.5 }
# print discounted_prices
# string_prices = ['5.50', '7.00', '6.00', '9.00', '4.50']
# puts "\nConvert string prices to integer and calculate price"
# discounted_prices = string_prices.map { |value| value.to_i }.map { |price| price - price * 0.5 }
# print discounted_prices
# 9. write another verison of the above discounted_prices calculation to use two
# separate map statements rather then one comboned statement
# 10. write another version of the above discounted_prices calculation to use each statements
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment