[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Strange code or Zerotons instead of Singletons

Robert Dober

11/27/2007 9:48:00 AM

Hi list

I am kind of attached to strange code in that moment, it somehow shows
up on it's own, I needed a singleton class and of course I had to
reinvent the wheel.
Actually, singletons do not always make sense, sometimes zerotons will
do the trick, so I created my zeroton

class << MyZero = Class::new( Whatever ) {
def a; 42 end
def new *args, &blk
self # or raise YourEyeBrows
end
}

and why not

module Kernel
def zeroton superclass = Object, &blk
returning( Class::new( superclass ) ) { |klass|
class << klass; self end.module_eval &blk
class << klass
def new *args, &blk
self
end
end
}
end
end

MyZero = zeroton( Whatever ) {
def a; 42 end
}

Any thoughts, insults (moderated though;) ?

Cheers
Robert
--

http://ruby-smalltalk.blo...

---
All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)

12 Answers

MonkeeSage

11/27/2007 12:52:00 PM

0

On Nov 27, 3:48 am, Robert Dober <robert.do...@gmail.com> wrote:
> Hi list
>
> I am kind of attached to strange code in that moment, it somehow shows
> up on it's own, I needed a singleton class and of course I had to
> reinvent the wheel.
> Actually, singletons do not always make sense, sometimes zerotons will
> do the trick, so I created my zeroton
>
> class << MyZero = Class::new( Whatever ) {
> def a; 42 end
> def new *args, &blk
> self # or raise YourEyeBrows
> end
>
> }

end

^ missing

> and why not
>
> module Kernel
> def zeroton superclass = Object, &blk
> returning( Class::new( superclass ) ) { |klass|
> class << klass; self end.module_eval &blk
> class << klass
> def new *args, &blk
> self
> end
> end
> }
> end
> end
>
> MyZero = zeroton( Whatever ) {
> def a; 42 end
>
> }
>
> Any thoughts, insults (moderated though;) ?
>
> Cheers
> Robert
> --
>
> http://ruby-smalltalk.blo...
>
> ---
> All truth passes through three stages. First, it is ridiculed. Second,
> it is violently opposed. Third, it is accepted as being self-evident.
> Schopenhauer (attr.)

I'm probably just dense, but couldn't you get the same thing with...

def zeroton(klass, &block)
klass.module_eval(&block) if block_given?; klass
end

Or...

module Kernel
def zeroton(klass, &block)
klass.module_eval(&block) if block_given?; klass
end
end

?

Regards,
Jordan

Trans

11/27/2007 2:17:00 PM

0



On Nov 27, 4:48 am, "Robert Dober" <robert.do...@gmail.com> wrote:
> Hi list
>
> I am kind of attached to strange code in that moment, it somehow shows
> up on it's own, I needed a singleton class and of course I had to
> reinvent the wheel.
> Actually, singletons do not always make sense, sometimes zerotons will
> do the trick, so I created my zeroton
>
> class << MyZero = Class::new( Whatever ) {
> def a; 42 end
> def new *args, &blk
> self # or raise YourEyeBrows
> end
>
> }
>
> and why not
>
> module Kernel
> def zeroton superclass = Object, &blk
> returning( Class::new( superclass ) ) { |klass|
> class << klass; self end.module_eval &blk
> class << klass
> def new *args, &blk
> self
> end
> end
> }
> end
> end
>
> MyZero = zeroton( Whatever ) {
> def a; 42 end
>
> }
>
> Any thoughts, insults (moderated though;) ?

Why are you using a class as if it were an instance?

What's wrong with:

MyZero = Whatever.new
def what.a; 42; end

T.

MonkeeSage

11/27/2007 2:32:00 PM

0

On Nov 27, 8:17 am, Trans <transf...@gmail.com> wrote:
> On Nov 27, 4:48 am, "Robert Dober" <robert.do...@gmail.com> wrote:
>
>
>
> > Hi list
>
> > I am kind of attached to strange code in that moment, it somehow shows
> > up on it's own, I needed a singleton class and of course I had to
> > reinvent the wheel.
> > Actually, singletons do not always make sense, sometimes zerotons will
> > do the trick, so I created my zeroton
>
> > class << MyZero = Class::new( Whatever ) {
> > def a; 42 end
> > def new *args, &blk
> > self # or raise YourEyeBrows
> > end
>
> > }
>
> > and why not
>
> > module Kernel
> > def zeroton superclass = Object, &blk
> > returning( Class::new( superclass ) ) { |klass|
> > class << klass; self end.module_eval &blk
> > class << klass
> > def new *args, &blk
> > self
> > end
> > end
> > }
> > end
> > end
>
> > MyZero = zeroton( Whatever ) {
> > def a; 42 end
>
> > }
>
> > Any thoughts, insults (moderated though;) ?
>
> Why are you using a class as if it were an instance?
>
> What's wrong with:
>
> MyZero = Whatever.new
> def what.a; 42; end
>
> T.

^ That was my thought too, but since he mentioned singleton, I thought
maybe he wanted a class object he could instantiate multiple times and
all of them would have whatever was in the block...like Whatever with
an anonymous mixin.

Regards,
Jordan

Robert Dober

11/27/2007 2:40:00 PM

0

On Nov 27, 2007 3:17 PM, Trans <transfire@gmail.com> wrote:
>
>
> On Nov 27, 4:48 am, "Robert Dober" <robert.do...@gmail.com> wrote:
>
> > Hi list
> >
> > I am kind of attached to strange code in that moment, it somehow shows
> > up on it's own, I needed a singleton class and of course I had to
> > reinvent the wheel.
> > Actually, singletons do not always make sense, sometimes zerotons will
> > do the trick, so I created my zeroton
> >
> > class << MyZero = Class::new( Whatever ) {
> > def a; 42 end
> > def new *args, &blk
> > self # or raise YourEyeBrows
> > end
> >
> > }
> >
> > and why not
> >
> > module Kernel
> > def zeroton superclass = Object, &blk
> > returning( Class::new( superclass ) ) { |klass|
> > class << klass; self end.module_eval &blk
> > class << klass
> > def new *args, &blk
> > self
> > end
> > end
> > }
> > end
> > end
> >
> > MyZero = zeroton( Whatever ) {
> > def a; 42 end
> >
> > }
> >
> > Any thoughts, insults (moderated though;) ?
>
> Why are you using a class as if it were an instance?
>
> What's wrong with:
>
> MyZero = Whatever.new
> def what.a; 42; end
I guess you mean def MyZero.a
and indeed that is probably what I need.
At least in the usecase I was working on I will go for this simple
solution, thx Tom.

What I want is a class though, probably wrongly, now I have to figure
out why....
Cheers
Robert
--

http://ruby-smalltalk.blo...

---
All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)

Robert Dober

11/27/2007 2:54:00 PM

0

On Nov 27, 2007 3:35 PM, MonkeeSage <MonkeeSage@gmail.com> wrote:
>
> On Nov 27, 8:17 am, Trans <transf...@gmail.com> wrote:
> > On Nov 27, 4:48 am, "Robert Dober" <robert.do...@gmail.com> wrote:
> >
> >
> >
> > > Hi list
> >
> > > I am kind of attached to strange code in that moment, it somehow shows
> > > up on it's own, I needed a singleton class and of course I had to
> > > reinvent the wheel.
> > > Actually, singletons do not always make sense, sometimes zerotons will
> > > do the trick, so I created my zeroton
> >
> > > class << MyZero = Class::new( Whatever ) {
> > > def a; 42 end
> > > def new *args, &blk
> > > self # or raise YourEyeBrows
> > > end
> >
> > > }
> >
> > > and why not
> >
> > > module Kernel
> > > def zeroton superclass = Object, &blk
> > > returning( Class::new( superclass ) ) { |klass|
> > > class << klass; self end.module_eval &blk
> > > class << klass
> > > def new *args, &blk
> > > self
> > > end
> > > end
> > > }
> > > end
> > > end
> >
> > > MyZero = zeroton( Whatever ) {
> > > def a; 42 end
> >
> > > }
> >
> > > Any thoughts, insults (moderated though;) ?
> >
> > Why are you using a class as if it were an instance?
> >
> > What's wrong with:
> >
> > MyZero = Whatever.new
> > def what.a; 42; end
> >
> > T.
>
> ^ That was my thought too, but since he mentioned singleton, I thought
> maybe he wanted a class object he could instantiate multiple times and
> all of them would have whatever was in the block...like Whatever with
> an anonymous mixin.
>
> Regards,
> Jordan
>
>
I guess I know what I wanted now, I wanted to be able to use the
"Zeroton" with #new because it fits "better" into the model, I have
a Node which can have leaves or empty leaves (still working on ropes I
am helpless ;).
If I go for the easy implementation my code will look as follows
@left = EmptyNode
@right = RopeLeaf.new(...)

and when I go with the more complicated the code will look as follows
@left = EmptyNode.new
@right = RopeLeaf.new(...)

probably the second is missleading anyway and I will just create a
constant EmptyNode object.

I still want to group the method defs into a block though

(1)

X = returning(Object::new){ |o|
o.instance_eval{
def a; 42 end
}
}
(2)
def zeroton &blk
returning( Object::new ){ |o|
o.instance_eval &blk
}
end

X=zeroton {
def a; 42 end
}
(3) # quite normal, but ...
X = Object.new
def X.a; 42 end

(4) # ... I hate those loose def Something.a, they are a maintenance nightmare
class << X = Object.new
def a; 42 end
end
I guess I have to give in for (3 or 4) I cannot come up with excuses
for being clever anymore.

Thx.
Robert




--

http://ruby-smalltalk.blo...

---
All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)

MonkeeSage

11/27/2007 4:38:00 PM

0

On Nov 27, 8:53 am, Robert Dober <robert.do...@gmail.com> wrote:

> I guess I have to give in for (3 or 4) I cannot come up with excuses
> for being clever anymore.

I mainly stick to simple stuff. I'm not a metaprogramming genius like
a lot of the folks around here. I just confuse myself and break stuff,
heh. But I do find it fascinating the things you can do with it. _why
does some funky stuff that makes my socks unravel. And I did find your
code interesting. :)

Regards,
Jordan

Robert Dober

11/27/2007 4:55:00 PM

0

On Nov 27, 2007 5:40 PM, MonkeeSage <MonkeeSage@gmail.com> wrote:
> On Nov 27, 8:53 am, Robert Dober <robert.do...@gmail.com> wrote:
>
> > I guess I have to give in for (3 or 4) I cannot come up with excuses
> > for being clever anymore.
>
> I mainly stick to simple stuff. I'm not a metaprogramming genius like
> a lot of the folks around here. I just confuse myself and break stuff,
> heh. But I do find it fascinating the things you can do with it. _why
> does some funky stuff that makes my socks unravel. And I did find your
> code interesting. :)
thx but the important lesson I learnt is to notice that indeed
metaprogramming was not needed :)

Cheers
Robert


--

http://ruby-smalltalk.blo...

---
All truth passes through three stages. First, it is ridiculed. Second,
it is violently opposed. Third, it is accepted as being self-evident.
Schopenhauer (attr.)

ara.t.howard

11/27/2007 4:55:00 PM

0


On Nov 27, 2007, at 2:48 AM, Robert Dober wrote:

> I am kind of attached to strange code in that moment, it somehow shows
> up on it's own, I needed a singleton class and of course I had to
> reinvent the wheel.

http://codeforp...lib/ruby/prototype/prototype-2....


note that prototypes can be dup'd or clone'd

i think it might be what you are looking for?

cheers.

a @ http://codeforp...
--
we can deny everything, except that we have the possibility of being
better. simply reflect on that.
h.h. the 14th dalai lama




Trans

11/27/2007 5:23:00 PM

0



On Nov 27, 9:40 am, "Robert Dober" <robert.do...@gmail.com> wrote:
>
> > MyZero = Whatever.new
> > def what.a; 42; end
>
> I guess you mean def MyZero.a

Yes.

You can def new on it:

def MyZero.new; self; end

What you want?

T.

MonkeeSage

11/27/2007 8:41:00 PM

0

I'm still thinking that class Whatever with a anonymous mixin (in the
form of a block) is the closest...

def zeroton(klass, &block)
klass.module_eval(&block) if block_given?; klass
end

Regards,
Jordan