'Is there a map function in Ruby? [duplicate]
I want to map values from a named list in Ruby. Is there an equivalent map function in Ruby? I have an array
letters = ['a', 'b', 'c', 'd']
capital = letters .map { l, l .capitalize] }
puts capital
Solution 1:[1]
Yes, Ruby arrays have a .map method that you can call (https://ruby-doc.org/core-2.7.5/Array.html#method-i-map).
What you probably want is:
letters = ['a', 'b', 'c', 'd']
capitals = letters.map {|letter| letter.capitalize}
or you could also use the shorter form:
letters = ['a', 'b', 'c', 'd']
capitals = letters.map(&:capitalize)
or maybe even use the .upcase instead of .capitalize if all you need for the result is uppercase.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Jarno Lamberg |
