Ruby – NoMethodError: undefined method `join’

rspecruby

So Here's the Ruby code I'm working on:

def translate(x)
 array = x.split(" ")
  array.each do |y|
    if y.match(/^[aeiou]/)
      y += "ay"
    else 
      until y.match(/^[aeiou]/) do
        var = y[/^[^aeiou]*/]
        y.slice! /^[^aeiou]*/
        y += (var + "ay")
      end
    end
  x = y.join(" ")
  x
end
end

There's an issue when I test it. It's this:

NoMethodError:
       undefined method `join' for "appleay":String

Not at all sure what the matter with my join method is.

Best Answer

y is a string. If you need make from it array of chars you should do:

x = y.split(//).join(" ")

but probably you want to place it after the loop. It will looks like:

def translate(x)
  array = x.split(" ")
  x = []
  array.each do |y|
    if y.match(/^[aeiou]/)
      y += "ay"
    else
      until y.match(/^[aeiou]/) do
        var = y[/^[^aeiou]*/]
        y.slice! /^[^aeiou]*/
        y += (var + "ay")
      end
    end
    x << y
  end
  x.join(' ')
end
Related Topic