The last expression evaluated in a method is implicitly returned. In this rev method, the last line is c.join(‘ ‘), which is the expression whose value you want to return. However, in this example, you’re not seeing the returned value because you’re only invoking the method rev(‘hello’), but you’re not doing anything with the returned value.

To capture the returned value and use it, you need to assign it to a variable or use it directly. For example:

result = rev('hello')
puts result

This will capture the returned value of rev(‘hello’) in the variable result, and then print it using puts.

Alternatively, you can directly use the returned value:

puts rev('hello')

Both of these approaches will print the reversed string “olleh”, which is the returned value of your rev method.

Support On Demand!

Ruby on Rails