[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

"Object:Array" - strange method invocation problem

Steve Conover

5/8/2006 8:51:00 PM

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

9 Answers

uncutstone

5/8/2006 11:20:00 PM

0

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

5/9/2006 12:09:00 AM

0

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?

uncutstone

5/9/2006 2:02:00 AM

0

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.

Steve Conover

5/9/2006 3:21:00 AM

0

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.

uncutstone

5/9/2006 4:48:00 AM

0

Sorry I cannot reoccur what you described.
I think you need give more source code, enough to show what you said.

Robert Klemme

5/9/2006 7:28:00 AM

0

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

heathweaver

5/9/2006 9:29:00 AM

0

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_mo...).

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.s...

Steve Conover

5/9/2006 5:37:00 PM

0

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

5/9/2006 5:38:00 PM

0

I of course meant

class AssociationCollection
def zap
to_ary.zap
end
end