2

I have a function toWords which converts a integer into a word

e.g. toWords(500, tableWords) gives fivehundred

I have an array of numbers h = (1..999).to_a, and I want to go through this array and convert each number into a word and store it in a new array. My current attempt to do this is:

h = (1..999).to_a
Lh = h.each do |i| toWords(i, tableWords) end

However, the contents of Lh is simply the integers from 1 to 999 and not the output of my toWords function. How do I do this? I'm thinking of something along the lines of sapply in R.

Even better is if my new array Lh can have two columns, the first column containing the integers in number format, and the second column would be the corresponding number in words.

Thank you!

1
  • 2
    Array.map Commented Mar 10, 2013 at 8:09

2 Answers 2

5

To get your two columns, you can do the following

(1..999).map {|x| [x, toWords(x, tableWords)]}
Sign up to request clarification or add additional context in comments.

Comments

2

As per Cicada's comment, the answer is:

Lh = h.map{|x| toWords(x, tableWords)}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.