Wondering the same thing myself. Jim, do you have an application?
Sorry I didn't make it. I wanted to come, but something came up.
While on the topic of singletons, this is how I make my singleton *objects*:
singletonObject = Class.new(base_class) { # base_class defaults to Object
def foo ... # or define_method
...
}.new
All this does is create an anonymous class and call new on the class to create
the singleton object (for that class). I find this more intuitive and readable
than dealing with meta classes.
This is also a great way to make a "duck" - make a custom object walk/talk like
some duck.
The meta class way to make a singleton object would be something like this:
singletonObject = base_class.new
class << singletonObject
def ...
...
end
or if you need to use define_method to get at things in your scope ("class"
creates an independent scope), it gets uglier:
singletonObject = base_class.new
singletonObjectMetaClass = (class << singletonObject; self; end)
singletonObjectMetaClass.send.define_method(...) {...}
...
- Eric
--- On Thu, 11/20/08, MarsHall <m@...> wrote:
> From: MarsHall <m@...>
> Subject: Re: [arctan] Double Meta
> To: arctan@yahoogroups.com
> Date: Thursday, November 20, 2008, 11:45 AM
> For those of us not in attendance to the meeting:
> Is this useful, or purely experimental Ruby?
>
> *Mars
>
>
> On Nov 19, 2008, at 22:46, Jim Freeze wrote:
>
> > For those that attended the meeting, here is my code
> where
> > I introduce a singleton class in a singleton class.
> >
> > class C
> >
> > class << self
> > class << self
> > def foo
> > "foo"
> > end
> > end
> >
> > def fred
> > "fred"
> > end
> >
> > def single
> > class << self
> > self
> > end
> > end
> > end
> > end
> >
> > p C.fred
> > p C.single.foo
> >
> > b = C.clone
> > p b.fred
> > p b.single.foo # undefined method foo
> >
> >
> >
> > --
> > Jim Freeze
>
>
> ( <> .. <> )
>
>
>
> ------------------------------------
>
> Yahoo! Groups Links
>
>
>