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
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
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.