Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > "Object:Array" - strange method invocation problem

Reply
Thread Tools

"Object:Array" - strange method invocation problem

 
 
Steve Conover
Guest
Posts: n/a
 
      05-08-2006
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

 
Reply With Quote
 
 
 
 
uncutstone
Guest
Posts: n/a
 
      05-08-2006
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?

 
Reply With Quote
 
 
 
 
Steve Conover
Guest
Posts: n/a
 
      05-09-2006
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?


 
Reply With Quote
 
uncutstone
Guest
Posts: n/a
 
      05-09-2006
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.

 
Reply With Quote
 
Steve Conover
Guest
Posts: n/a
 
      05-09-2006
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.

 
Reply With Quote
 
uncutstone
Guest
Posts: n/a
 
      05-09-2006
Sorry I cannot reoccur what you described.
I think you need give more source code, enough to show what you said.

 
Reply With Quote
 
Robert Klemme
Guest
Posts: n/a
 
      05-09-2006
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

 
Reply With Quote
 
heathweaver
Guest
Posts: n/a
 
      05-09-2006
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

 
Reply With Quote
 
Steve Conover
Guest
Posts: n/a
 
      05-09-2006
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

 
Reply With Quote
 
Steve Conover
Guest
Posts: n/a
 
      05-09-2006
I of course meant

class AssociationCollection
def zap
to_ary.zap
end
end

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
XML-RPC WEBrick problem (error during method invocation) Nikita Kuznetsov Ruby 9 10-07-2010 03:25 PM
problem with multithreading and Web services method invocation abhra.haldar@gmail.com ASP .Net 1 05-11-2006 04:21 PM
MasterPage Method Invocation from ContentPage User ASP .Net 9 12-19-2005 11:20 AM
java.io.BufferedWriter and java.io.PrintStream - a seeming parameter/argument mismatch in method invocation Novice Java 1 07-19-2003 06:46 PM
Re: Help: Method invocation using an automation tool. D. Lane Java 0 06-27-2003 10:40 PM



Advertisments