On 7/18/06, Mauricio Fernandez <mfp / acm.org> wrote:
> On Sat, Jul 15, 2006 at 10:33:09AM +0900, Sean O'Halpin wrote:
> > On 7/14/06, ara.t.howard / noaa.gov <ara.t.howard / noaa.gov> wrote:
> > >On Fri, 14 Jul 2006, Sean O'Halpin wrote:
> > In most cases, this works:
> >
> > class Foo
> >  def self.open(*args, &block)
> >    p [args, block]
> >    yield if block_given?
> >  end
> > end
> [...]
> > (class << Foo; self; end).instance_eval do
> >  define_method(:open, open_m)
> > end
> [...]
> > but as we've seen, it doesn't work in the case of the File(IO)
> > singleton. To be honest, I'm not clear about exactly what kind of
> > object the File(IO) singleton is. Anyone offer any enlightenment?
>
> Open is a singleton method of IO, not File; this is why the call fails (in
> 1.9, it'd happen earlier, as soon as you try to rebind the method).  There's
> nothing that special about File itself:
>
> class X
>   def self.foo; yield + 1 end
> end
>
> class Y < X
> end
>
> Y.foo{1}                  # => 2
> m = Y.method(:foo)
> def Y.foo; 10 end
> Y.foo{1}                  # => 10
> class << Y; self end.class_eval{define_method(:foo, m)}
> Y.foo{1}                  # =>
> # ~> -:13:in `foo': singleton method bound for a different object (TypeError)
> # ~>    from -:13
>
> As for the File(IO) notation, it seems to mean that the singleton method
> belongs to IO:
>
> class X; def self.foo; yield + 1 end end
> class Y < X; end
> Y.method(:foo)                  # => #<Method: Y(X).foo>
> X.method(:foo)                  # => #<Method: X.foo>
>
> Keep in mind that the singleton class of a class is derived from that of its
> superclass:
>
> class X; def self.foo; 1 end end
> class Y < X; end
> X.methods(false)        # => ["foo"]
> Y.methods(false)        # => []
> Y.foo                   # => 1
>
> --
> Mauricio Fernandez  -   http://eigenclass.org   -  singular Ruby
>
>
I think that certainly fits the description of enlightenment :)

I can see now that my example was missing the essential aspect of
using a parent's singleton method in a derived class.

And so back to Ara's challenge...

Regards,
Sean