"Object:Array" - strange method invocation problem

S

Steve Conover

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
 
S

Steve Conover

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
 
U

uncutstone

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

Steve Conover

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

uncutstone

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

Robert Klemme

Steve said:
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
 
H

heathweaver

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
 
S

Steve Conover

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top