"Object:Array" - strange method invocation problem

Discussion in 'Ruby' started by Steve Conover, May 8, 2006.

  1. Hi everyone,

    I have a strange problem mixing in methods with the same name into
    Object and Array (and I'm assuming this is a more general issue than
    just both those classes).

    My code is something like this:

    class Object
    def zap
    puts "in object"
    end
    end


    class Array
    def zap
    puts "in array"
    end
    end

    I'm recieving an object instance from another part of the code, and
    when I check its class it's "Object:Array". First of all, what is
    "Object:Array" as opposed to "Object" and "Array"?

    Secondly, when I invoke myobj.zap, it prints "in object". What's weird
    is that if I add in these debugging statements:


    class Object
    def zap
    puts self.method:)zap).to_s
    puts self.method:)zap).call
    puts "in object"
    end
    end

    It reports that it's the method instance from class Array, and invoking
    "call" actually causes the Array.zap method to execute.

    I'm at a loss, and am very curious about what's going on here. Any
    ideas?

    Regards,
    Steve
     
    Steve Conover, May 8, 2006
    #1
    1. Advertising

  2. Steve Conover

    uncutstone Guest

    Steve Conover wrote:
    >I'm recieving an object instance from another part of the code


    Would you please give the code creating the object instance?
     
    uncutstone, May 9, 2006
    #2
    1. Advertising

  3. It's actually an instance of a has_many rails attribute. e.g.
    department.employees

    I'm about to go look at what specifically rails does to create these
    Arrays...however I was hoping this could be explained (for instance,
    what "Object:Array" is) in more general ruby terms, just because I'm
    curious - I've never come across this.

    -Steve

    uncutstone wrote:
    > Steve Conover wrote:
    > >I'm recieving an object instance from another part of the code

    >
    > Would you please give the code creating the object instance?
     
    Steve Conover, May 9, 2006
    #3
  4. Steve Conover

    uncutstone Guest

    How do you check class of the object instance?
    I do think you don't give enough information.

    Object:Array seems mean a parent child relationship.
     
    uncutstone, May 9, 2006
    #4
  5. It's Array, the methods on it are all from array, yet when invoked
    directly (not through Method.call) they act as if they're from Object.
     
    Steve Conover, May 9, 2006
    #5
  6. Steve Conover

    uncutstone Guest

    Sorry I cannot reoccur what you described.
    I think you need give more source code, enough to show what you said.
     
    uncutstone, May 9, 2006
    #6
  7. Steve Conover wrote:
    > Hi everyone,
    >
    > I have a strange problem mixing in methods with the same name into
    > Object and Array (and I'm assuming this is a more general issue than
    > just both those classes).
    >
    > My code is something like this:
    >
    > class Object
    > def zap
    > puts "in object"
    > end
    > end
    >
    >
    > class Array
    > def zap
    > puts "in array"
    > end
    > end
    >
    > I'm recieving an object instance from another part of the code, and
    > when I check its class it's "Object:Array". First of all, what is
    > "Object:Array" as opposed to "Object" and "Array"?
    >
    > Secondly, when I invoke myobj.zap, it prints "in object". What's weird
    > is that if I add in these debugging statements:
    >
    >
    > class Object
    > def zap
    > puts self.method:)zap).to_s
    > puts self.method:)zap).call
    > puts "in object"
    > end
    > end
    >
    > It reports that it's the method instance from class Array, and invoking
    > "call" actually causes the Array.zap method to execute.
    >
    > I'm at a loss, and am very curious about what's going on here. Any
    > ideas?
    >
    > Regards,
    > Steve
    >


    <disclaimer>No Rails knowledge here.</disclaimer>

    One possible explanation is that the instance you have is not an Array
    but maybe a wrapper around Array with some Rails logic. The wrapper
    then uses Object::zap if invoked directly but Array::zap if invoked
    through method:)zap).

    robert
     
    Robert Klemme, May 9, 2006
    #7
  8. Steve Conover

    heathweaver Guest

    I ran into a similar problem when I was trying to override the
    titlecase method of ActiveSupport. To solve the issue I had to read up
    on mixins and that answered my problem
    (http://www.rubycentral.com/book/tut_modules.html).

    I wasn't able to figure out what rails was doing, but in the end I just
    created a file with String class definition then mixed in a library
    with the new titlecase method.

    Not sure if that will help you, just thought I'd offer it up.

    heath
    http://development.smush.co.uk
     
    heathweaver, May 9, 2006
    #8
  9. The Rails-specific fix for this was to define a zap method on
    AssociationCollection:

    def AssociationCollection
    def zap
    to_ary.zap
    end
    end

    I'm still not exactly clear on why the original zap method reported
    that it was from Array. I just need to study up more on what Rails is
    doing to create these association objects I guess. The class
    "Object:Array" is still a mystery to me. But my problem is solved at
    least.

    Thanks everyone.

    -Steve
     
    Steve Conover, May 9, 2006
    #9
  10. I of course meant

    class AssociationCollection
    def zap
    to_ary.zap
    end
    end
     
    Steve Conover, May 9, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. User
    Replies:
    9
    Views:
    10,466
  2. D. Lane
    Replies:
    0
    Views:
    788
    D. Lane
    Jun 27, 2003
  3. John N.

    inherited method invocation

    John N., Oct 23, 2003, in forum: Java
    Replies:
    2
    Views:
    569
    Joona I Palaste
    Oct 23, 2003
  4. Replies:
    1
    Views:
    3,184
    bruce barker \(sqlwork.com\)
    May 11, 2006
  5. Nikita Kuznetsov
    Replies:
    9
    Views:
    196
    Jeremy Bopp
    Oct 7, 2010
Loading...

Share This Page