Strange code or Zerotons instead of Singletons

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.blogspot.com/


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

On Nov 27, 3:48 am, Robert D. [email protected] wrote:

def new *args, &blk
self # or raise YourEyeBrows
end

}

end

^ missing

        end

Cheers
Robert

http://ruby-smalltalk.blogspot.com/


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

On Nov 27, 4:48 am, “Robert D.” [email protected] wrote:

def new *args, &blk
class << klass; self end.module_eval &blk
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.

On Nov 27, 8:17 am, Trans [email protected] wrote:

do the trick, so I created my zeroton

end

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

On Nov 27, 2007 3:17 PM, Trans [email protected] wrote:

do the trick, so I created my zeroton

end

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.blogspot.com/


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

On Nov 27, 2007 3:35 PM, MonkeeSage [email protected] wrote:

reinvent the wheel.

        end

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.blogspot.com/


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

On Nov 27, 2007 5:40 PM, MonkeeSage [email protected] wrote:

On Nov 27, 8:53 am, Robert D. [email protected] 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. :slight_smile:
thx but the important lesson I learnt is to notice that indeed
metaprogramming was not needed :slight_smile:

Cheers
Robert

http://ruby-smalltalk.blogspot.com/


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

On Nov 27, 8:53 am, Robert D. [email protected] 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. :slight_smile:

Regards,
Jordan

On Nov 27, 2007, at 2:48 AM, Robert D. 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://codeforpeople.com/lib/ruby/prototype/prototype-2.0.0/README

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

i think it might be what you are looking for?

cheers.

a @ http://codeforpeople.com/

On Nov 27, 9:40 am, “Robert D.” [email protected] 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.

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

On Nov 27, 2007 6:23 PM, Trans [email protected] wrote:

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

http://ruby-smalltalk.blogspot.com/


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

On Nov 27, 2007 9:45 PM, MonkeeSage [email protected] wrote:

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

http://ruby-smalltalk.blogspot.com/


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