SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2011
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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, "\n"}
    [4, 13, 18, "fred", "alice"].reach {|x| print x, "\n"}
    [4, [13, 88], [19, "fred", "snark"], "alice"].each { |x| print x, "\n"}
    [4, [13, 88], [19, "fred", "snark"], "alice"].reach { |x| print x, "\n"}

  2. #2
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code Ruby:
    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:

    Code Ruby:
    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


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •