I'm a Ruby Neb

Below is a good example of someone brand new to Ruby (me!) and someone with Ruby experience (not me!) solving the exact same problem. These two methods have the same goal: accept a string, reverse and return it.

def solution(sentence)

  sentence_as_array = sentence.split(" ").reverse!

  reversed_string = ""
  sentence_as_array.each do |x|

    reversed_string = reversed_string + x + " "

  end

  return reversed_string.chop

end

def better_solution(sentence)

  sentence.split(" ").reverse.join(" ")

end

It's crazy how much more elegant the second method is.

More Posts About... programming / ruby on rails