Robert Dober
4/22/2007 9:17:00 AM
On 4/22/07, Philipp Taprogge <Philipp.Taprogge@gmx.net> wrote:
> Hi!
>
> Thus spake apefan@dodgeit.com on 04/21/2007 07:31 PM:
> > Baz#boo and Baz.new.method(:boo).call dosn't produce the same result.
> > Does this surprise anyone?
>
> As far as I understand ruby's dynamic typing, that output is just
> what you would expect:
>
> When you create a new Baz and call it's boo(), you are actually
> holding a class instance to do the dispatching: first the
> module-included boo() is run and that looks for a boo() in the
> _caller's_, i.e. this particular Bar's, superclass.
>
> In the second case, you create a Baz, but then get hold of the
> method itself by a process called "reflection" or "introspection".
> Since everything in ruby is an object, that's what you get: a method
> object representing the method itself.
> So, in this case, you are holding the raw boo() method directly from
> your Bar module. This method object does not know anything at all
> about the instance that provided it, i.e. the new Baz you called
> method() on.
Sorry but this is not true, we get a method object bound to the
instance which can be called without any problem. Look at this code,
just to convince you
512/12 > cat simple.rb && ruby simple.rb
# vim: sts=2 sw=2 expandtab tw=0 nu:
class Baz
def initialize
@x = 42
end
def boo
puts "Baz::boo @x=#{@x}"
end # def boo
end
Baz.new.boo
Baz.new.method(:boo).call
Baz::boo @x=42
Baz::boo @x=42
> It is just a sniplet of code, wrapped in an object for you.
well you are saying it yourself here ;)
> Now, since Bar itself does not have a superclass, a call to super
> _in_this_context_ yields the NoMethodError you encountered.
It does not seem to be right super should just work fine as we are in
the context of a Baz object and it can search the
self.class.ancestors list, which is
[Baz, Boo, Foo, Object, Kernel]
in both cases, just put
puts self.class.ancestors.inspect
into Boo::boo
The behavior seems wrong to me too , but hopefully a more learned
member of the list will enlighten us.
Cheers
Robert
>
> To cut a long story short,
<snip> ;)
>
Robert
--
You see things; and you say Why?
But I dream things that never were; and I say Why not?
-- George Bernard Shaw