Take the 2-minute tour ×

Here's the code I have which takes a string and reverse it

    name = "I CAN REVERSE THIS"

    def reverse_string(name_string)
      string_arr = []
      string_arr = name_string.split('')

      for i in 1..(string_arr.length)
        new_string = "#{new_string}#{string_arr[-i]}"

      #return new_string

    new_name = reverse_string(name)
    puts new_name

Why does my reverse_string function returns "1..18" if no return actions is called inside the function, but returns expected results if return action is called?

share|improve this question
Are you trying to rewrite reverse method?? –  bjhaid Feb 26 '12 at 12:56
New to Ruby; was just exploring; seems like best way to learn is to rewrite simple methods. –  ZeTradr Feb 27 '12 at 15:52
add comment

1 Answer

up vote 1 down vote accepted

In Ruby, a method's implicit return value (if no explicit call to return is being made) is the return value of the last statement in that method. In this case that is the for, which apparently returns what it iterated over.

share|improve this answer
make sense; Thanks ! –  ZeTradr Feb 26 '12 at 8:29
add comment

Your Answer


