Getting Error with local variable

class String
remove_method(:each)
end

class Object
  def reach
    if(x.respond_to?(:each))
      self.each{|x| x.reach(yield)}
    else
      yield(self)
    end
  end
end

I am getting the error, can’t find the local variable ‘x’… please help

test examples for code:
[4, 13, 18, “fred”, “alice”].each { |x| print x, "
"}
[4, 13, 18, “fred”, “alice”].reach {|x| print x, "
"}
[4, [13, 88], [19, “fred”, “snark”], “alice”].each { |x| print x, "
"}
[4, [13, 88], [19, “fred”, “snark”], “alice”].reach { |x| print x, "
"}

if x.respond_to?

ruby is telling you that the x in that line does not refer to anything. You need ‘self’ instead of x.

Also, don’t ever post code again without proper indenting. In the future, paste your code in the window, then highlight it and select the proper code tags from the drop down list(you may have to hit the ‘Go Advanced’ button to see the Select Syntax drop down list), and the forum software will automatically preserve the indenting, like this:

class String
  remove_method(:each)
end


class Object

  def reach
    if(x.respond_to?(:each))
      self.each{|x| x.reach(yield)}
    else
      yield(self)
    end
  end

end