C
Chad Lester
Ruby pop quiz. How can it be that:
foo.bar()
is different from:
foo.methodbar).call()
Is it possible to override the "method" method?
Can you also override the "class" method?
if foo.class == Array
Array.instance_methodfind_all).bind(foo)
end
Results in
TypeError: bind argument must be an instance of Array
Can you override all of the introspection methods so that there is NO
way to inspect an object and really tell what it is? If so, then how
did "bind" know the difference? There has to be a back door, right?
I found some "evil" at:
http://eigenclass.org/hiki.rb?class+hierarchy+introspection+evil.rb
Is this the best way in Ruby? Python reserved methods with two
underscores that can't be redefined. Does Ruby have an equivalent?
From the FAQ:
-------------
7.1 How does Ruby choose which method to invoke?
Ruby binds all messages to methods dynamically. It searches first for
singleton methods in the receiver, then for methods defined in the
receiver's own class, and finally for methods defined in the receiver's
superclasses (including any modules which may have been mixed in). You
can see the order of searching by displaying Classname.ancestors, which
shows the ancestor classes and modules of ClassName.
-----------
If this is true, then I only need to check three places to see what
method will be called. Is there a way for me to look the same way Ruby
does?
For those that are curious.... the real life example that raised this
question for me was in rails. When you have a Model with a has_many
relationship it creates an Array-like object that looks very much like
an Array, but behaves as described above (with a secret undocumented
deprecated find_all) method.
foo.bar()
is different from:
foo.methodbar).call()
Is it possible to override the "method" method?
Can you also override the "class" method?
if foo.class == Array
Array.instance_methodfind_all).bind(foo)
end
Results in
TypeError: bind argument must be an instance of Array
Can you override all of the introspection methods so that there is NO
way to inspect an object and really tell what it is? If so, then how
did "bind" know the difference? There has to be a back door, right?
I found some "evil" at:
http://eigenclass.org/hiki.rb?class+hierarchy+introspection+evil.rb
Is this the best way in Ruby? Python reserved methods with two
underscores that can't be redefined. Does Ruby have an equivalent?
From the FAQ:
-------------
7.1 How does Ruby choose which method to invoke?
Ruby binds all messages to methods dynamically. It searches first for
singleton methods in the receiver, then for methods defined in the
receiver's own class, and finally for methods defined in the receiver's
superclasses (including any modules which may have been mixed in). You
can see the order of searching by displaying Classname.ancestors, which
shows the ancestor classes and modules of ClassName.
-----------
If this is true, then I only need to check three places to see what
method will be called. Is there a way for me to look the same way Ruby
does?
For those that are curious.... the real life example that raised this
question for me was in rails. When you have a Model with a has_many
relationship it creates an Array-like object that looks very much like
an Array, but behaves as described above (with a secret undocumented
deprecated find_all) method.