Strange code or Zerotons instead of Singletons

R

Robert Dober

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
 
M

MonkeeSage

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

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
 
T

Trans

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.
 
M

MonkeeSage

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
 
R

Robert Dober

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
 
R

Robert Dober

^ 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
 
M

MonkeeSage

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
 
R

Robert Dober

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
 
M

MonkeeSage

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
 
R

Robert Dober

Yes.

You can def new on it:

def MyZero.new; self; end

What you want?
As I stated above my initial approach was overkill and you made me
think that the zeroton would not necessarily need new defined;
my preferred solution up to now is the following

class << MyZero = Whatever.new
def a; 42 end
# def new; self end # in case it is meaningful in the usecases
end

I took therefore your idea of creating a singleton object and
refraining from defining a method for doing this, the only
point where I adhere to my approach is the class << ... end
notation as I dislike def X.something very much, it just does not seem
DRY enough.
Thx for your input again.
Robert
 
R

Robert Dober

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
that is indeed very close to my final choice which is an inline expanded
def zeroton(object, &block)
class << self; self end.module_eval &block
object
end
<--->
class << X = "42"
def a; 43 end # ooops
end

the only reason why I shied away from class to non class objects is
the need to override #new in class objects.
By using Object::new or "42" as the receiver the intent of the
"zeroeness" becomes somehow clearer and I have to admit that I trust
Tom's instinct a lot, (I am a sleek guy am I not;)

Cheers
Robert
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,962
Messages
2,570,134
Members
46,692
Latest member
JenniferTi

Latest Threads

Top