-3

I'm a ruby beginner and would like some help!

Let's say we have the following array:

codes = [65, 66, 67, 68, 70, 84]

What ruby code do we have to call, to change this array into ["A", "B", "C", "D", "E", "F"]?

I want to use the chr method

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • 1
    `codes.map(&:chr)` – falsetru May 14 '17 at 04:09
  • 2
    [so] is not a write-my-code-for-me-service. (Those *do* exist, they are called "programmers", and you can hire them for a fee.) You need to show what you have tried, what is and isn't working. Please, provide a [mcve], a specification of what your desired behavior is (including any and all edge cases, e.g. what should happen with an empty array, what should happen with a number that is outside the range of Unicode code points, etc.), and give example inputs and desired outputs demonstrating both normal operation and edge cases. Also, take the [tour] and read the [help/on-topic]. – Jörg W Mittag May 14 '17 at 04:11

1 Answers1

0

You want Array#map.

[65, 66, 67, 68, 70, 84].map { |number| number.chr }
=> ["A", "B", "C", "D", "F", "T"]

And it's more idiomatic to use Symbol#to_proc:

[65, 66, 67, 68, 70, 84].map(&:chr)
=> ["A", "B", "C", "D", "F", "T"]
Community
  • 1
  • 1
burnettk
  • 13,557
  • 4
  • 51
  • 52
  • of note, [Symbol#to_proc](https://github.com/JuanitoFatas/fast-ruby/blob/master/code/proc-and-block/block-vs-to_proc.rb) is faster. – Josh Brody May 14 '17 at 07:06