-2

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
Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
  • 3
    Googling "Ruby map" generated many, many hits that answer your question, the first being ["How to Use The Ruby Map Method (With Examples)"](https://www.rubyguides.com/2018/10/ruby-map-method/). – Cary Swoveland May 18 '22 at 17:12

1 Answers1

3

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.

Jarno Lamberg
  • 1,530
  • 12
  • 12