1

I have a question about Ruby return values which is baffling me.

I have written a method which takes an array as an argument and formats the array into a list as follows:


    def list(array)
        array.each { |name, age| puts name + " is #{age} years old" }
    end

Let's say the array is [["Amy", 6], ["Tabitha", 5], ["Marcus", 9]].

I want this list method to return the strings in the do/end block, and not return the array. However, the return value is always an array.

I have tried assigning the block to a variable and returning the variable but it doesn't work. I also tried replacing puts with return but that then exits the method after the first iteration. Can't seem to work out what the problem is?

Sorry if this is a really silly question - I haven't come across it before.

Any input much appreciated, thanks! :)

1
  • 1
    each executes the given block for each element of the array, then returns the array itself. puts returns nil. You have to change each in to map and remove puts for this to work Commented Jul 5, 2019 at 11:09

2 Answers 2

6

You want map, not each. map is like each, except it returns the result of each block evaluation. each returns the original array. Also, and your main error here is that puts display something in the console, nothing else.

Your final code would be :

def list(array)
    array.map { |name, age| "#{name} is #{age} years old" }
end
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you, I tried this but it then just returns an array of nil values. So when I used the method you suggested I got the following output in my terminal: Amy is 6 years old Tabitha is 5 years old Marcus is 9 years old => [nil, nil]
jordan, when how_old? is defined this way how_old?([["Amy", 6], ["Tabitha", 5], ["Marcus", 9]]) returns ["Amy is 6 years old", "Tabitha is 5 years old", "Marcus is 9 years old"].
You might have forgotten to remove puts. puts displays its arguments, and returns nil. You want to return the string, not display it.
0

The return value of each is the list. The function returns that list since it is the last returnable instruction. You need to use the map method in the following way:

def how_old(people)
  people.map { |name, age| "#{name} is #{age} years old" }
end

4 Comments

Thank you, I tried this but it then just returns an array of nil values. So when I used the method you suggested I got the following output in my terminal: Amy is 6 years old Tabitha is 5 years old Marcus is 9 years old => [nil, nil]
How does this differ from @colinux's earlier answer?
Also, per Ruby convention, the function should not be named with a ?, as it doesn't answer a yes-no question.
There is no significant difference with @colinux's response, we did the same, your answer came before. @Amadan you are right, I will remove the symbol ?. I renamed the functions just to give some semantics.

Your Answer

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