OpenStruct#update ?

T

trans. (T. Onoma)

| > On Monday 15 November 2004 12:28 pm, Yukihiro Matsumoto wrote:
| > | I'd call them singleton classes. Any objection?
| > |
| > | matz.
| >
| > Yes, cause of singleton pattern.
|
| Objection to the objection: The singleton pattern is not widely used in
| Ruby. Couldn't we rename the implementation of it? Maybe something like
| "SingleInstanceClass"?

Well, I just used it. And the rest of the OOP world knows Singleton, yes?

T.
 
F

Florian Gross

trans. (T. Onoma) said:
| > On Monday 15 November 2004 12:28 pm, Yukihiro Matsumoto wrote:
| > | I'd call them singleton classes. Any objection?
| > |
| > | matz.
| >
| > Yes, cause of singleton pattern.
|
| Objection to the objection: The singleton pattern is not widely used in
| Ruby. Couldn't we rename the implementation of it? Maybe something like
| "SingleInstanceClass"?

Well, I just used it. And the rest of the OOP world knows Singleton, yes?

What did you use it for and why would another name be a problem in your
case?
 
R

Robert Klemme

Florian Gross said:
Robert said:
The singleton class of f knows about instance methods defined in super
classes. Which is logical, considering this:

Ah, I was confusing instance_methods() with instance_methods(false).
accessors = h.keys - o.methods
class << o; self; end.send:)attr_accessor, *accessors)
h.each do |key, value|
o.send("#{key}=", value) if accessors.include?(key)
end

Nice and short. But it has some drawbacks:
[...]

You would want ":bar" removed from the keys but it isn't because
f.methods
returns an array of String. (I assume that symbols are the most likely
keys for the hash - which might be wrong.)

Hm, I assumed the hash would contain Strings as keys. I guess it could be
normalized to that.

I'd prefer symbols - partly because they are more memory efficient and
partly because method id's are symbols, too. (Yeah, I know that all
#methods returns strings and send() et. al can deal with strings, too. But
I prefer to think of an identifier as a symbol rather as a string.)
I was trying to work around methods in Kernel -- I don't think it makes
sense to have o.p = when you don't have o.p.

Well, normall not. But the other way round is more likely to occur, i.e.,
you don't have a setter but you have a getter.
But I guess the check isn't strictly needed anyway. (attr_accessor :p will
create a public getter. I thought it would create a private one.)

Sorry, I'm not sure whether I understand what you mean here. Could you
please explain a bit more?

Kind regards

robert
 
G

Gavin Sinclair

On Monday 15 November 2004 12:28 pm, Yukihiro Matsumoto wrote:
| Hi,
|
| In message "Re: Kernel#singleton_class"
|
| on Mon, 15 Nov 2004 22:45:31 +0900, "David A. Black"
<[email protected]> writes:
| |>> I guess it's probably best to let matz decide the name.
| |
| |Yeah, as long as he doesn't choose 'virtual' :)
|
| I'd call them singleton classes. Any objection?
|
| matz.
Yes, cause of singleton pattern.

No, because of historical reasons, and the ability to distinguish
between two different uses of "singleton". (Besides, the Singleton
Pattern has fallen foul of OO purists.)

I say go for it, Matz.

Gavin
 
F

Florian Gross

Gavin said:
(Besides, the Singleton Pattern has fallen foul of OO purists.)

Got any URLs? I think I agree with them, but they might have better
reasons than me so I would be interested in reading about them. Thanks!
 
D

David A. Black

Hi --

| |>> I guess it's probably best to let matz decide the name.


No, because of historical reasons, and the ability to distinguish
between two different uses of "singleton". (Besides, the Singleton
Pattern has fallen foul of OO purists.)

If there's a conflict, though, it's got to do with the existence of
the Singleton module in Ruby, popular or not :) I do find myself
sometimes starting to write "singleton class", and then realizing that
that could be this:

class << obj; self; end

or this:

class C
include 'singleton'
end

I don't think this should be the deciding factor, but I do think that
we'll have lots of "not to be confused with..." explanations as long
as they both have this name.


David
 
J

James Edward Gray II

Got any URLs? I think I agree with them, but they might have better
reasons than me so I would be interested in reading about them.
Thanks!

I'm NOT an expert, but my opinion is that the pattern is overused.
There are cases where you must ensure restricted creation, but often
it's an artificial limitation. It closes down options, so I feel it
should only be used when you really need some significant protection it
provides. I'm a keep my options open kind of guy.

Again, that's 100% my opinion. Take it or leave it.

James Edward Gray II
 
F

Florian Gross

James said:
I'm NOT an expert, but my opinion is that the pattern is overused.
There are cases where you must ensure restricted creation, but often
it's an artificial limitation. It closes down options, so I feel it
should only be used when you really need some significant protection it
provides. I'm a keep my options open kind of guy.

Again, that's 100% my opinion. Take it or leave it.

Thanks for it, it is of help.
 
T

trans. (T. Onoma)

| > On Monday 15 November 2004 01:23 pm, Florian Gross wrote:
| > | trans. (T. Onoma) wrote:
| > | > On Monday 15 November 2004 12:28 pm, Yukihiro Matsumoto wrote:
| > | > | I'd call them singleton classes. Any objection?
| > | > |
| > | > | matz.
| > | >
| > | > Yes, cause of singleton pattern.
| > |
| > | Objection to the objection: The singleton pattern is not widely used in
| > | Ruby. Couldn't we rename the implementation of it? Maybe something like
| > | "SingleInstanceClass"?
| >
| > Well, I just used it. And the rest of the OOP world knows Singleton, yes?
|
| What did you use it for and why would another name be a problem in your
| case?

In current project I use it for markup BaseAdapter Subclasses. In past I've
used it for database pool, object pool, and CGI Singleton. There are others
too that I can't recall off the top of my head.

I actually started a thread on this matter a few weeks ago, David brought up
some good points. The nice thing about a singleton is that it is easily
initialized. Pretty sure the implementation is also thread safe. Two big
pluses in my book. Also, from a different vantage, singletons are nothing
more than fully unique multitons. So are multitons also outmoded? And, if
what you say is so, then what do other languages, lacking Ruby's proxy class,
use in their stay? Should't such a "pattern" be common ground across OOPLs?

The problem I have is very simple: constant reoccuring confusion in
discussions. Look back just a few post --it's even happened in this thread!
And it was you who once again had to explain:

<quote>
Sorry, I was talking about the pattern that lets you only create a
single instance of a class. See singleton.rb in Ruby's standard library.
</quote>

Don't you get tired of doing this? Honestly, I see this same kind of thing
every time singleton comes up. There was a mention of singleton in the
current DI thread, and guess what? I had no idea which one was meant. And
changing the name of the module for Singleton to SingletonPattern or
Pattern::Singleton is not going to help this problem at all.

Now, there is precedence for changing the name of singletons (the special per
object class kind) in that they have been called othe things (even Ruby
source code!), namely, meta class and virtual class. So the terminology is
still wavering --hence our conversation. Of course there are also certain
issues witht those two terms so something else would be prefereable. But in
contrast, singleton, as in the pattern, is pretty well set in OOP stone
(despite potential older Lisp usage). So why not just get on with it, change
the name, and be done with the whole issue.

Once you start using it, it will become second nature in short order. Perhaps
"trying it on for size" will help. Here's the relevant section of Pickaxe I
using the term 'especial-class' (hyphenated) rather than potentially
confusing term 'singleton class'.


Object-Specific Classes

Ruby allows you to create a class tied to a particular object. In the
following example, we create two String objects. We then associate an
anonymous class with one of them, overriding one of the methods in the
object's base class and adding a new method.

a = "hello"
b = a.dup
class <<a
def to_s
"The value is '#{self}'"
end
def twoTimes
self + self
end
end
a.to_s » "The value is 'hello'"
a.twoTimes » "hellohello"
b.to_s » "hello"

This example uses the ``class << obj'' notation, which basically says ``build
me a new class just for object obj.'' We could also have written it as:

a = "hello"
b = a.dup
def a.to_s
"The value is '#{self}'"
end
def a.twoTimes
self + self
end
a.to_s » "The value is 'hello'"
a.twoTimes » "hellohello"
b.to_s » "hello"

The effect is the same in both cases: a class is added to the object ``a''.
This gives us a strong hint about the Ruby implementation: an especial-class
is created and inserted as a's direct class. a's original class, String, is
made this especial's superclass. The before and after pictures are shown in
Figure 19.3 on page 242.

Ruby performs a slight optimization with these especial-classes. If an
object's klass reference already points to an especial-class, a new one will
not be created. This means that the first of the two method definitions in
the previous example will create an especial-class, but the second will
simply add a method to it.


T.
 
T

trans. (T. Onoma)

On Monday 15 November 2004 06:52 pm, Gavin Sinclair wrote:
| No, because of historical reasons, and the ability to distinguish
| between two different uses of "singleton". (Besides, the Singleton
| Pattern has fallen foul of OO purists.)
|
| I say go for it, Matz.

I have an idea. How about a compromise. Rather then use the term 'singleton
class' (the per object kind), lets use the slightly different term,
'singular-class'. That's quite nearly the same, but is just different enough
to prevent confusion.

Would that be alright with everyone? Matz?

T.
 
Z

Zach Dennis

I am curious how I can capture a block that was passed. Here's what I'm
trying to do:

class BaseClass
def initialize( arg1 )
puts "got a block" if block_given?
end
end

class SubBaseClass < BaseClass
def initialize( arg1 , arg2 )
super( arg1 )
end
end

sbc = SubBaseClass.new( "aarg1" , "arg2" ) { puts "my block" }


I would like BaseClass to receive the block I passed to the SubBaseClass
constructor. I have tried a few variations, but nothing went.

Zach
 
D

David A. Black

Hi --

I am curious how I can capture a block that was passed. Here's what I'm
trying to do:

class BaseClass
def initialize( arg1 )
puts "got a block" if block_given?
end
end

class SubBaseClass < BaseClass
def initialize( arg1 , arg2 )
super( arg1 )
end
end

sbc = SubBaseClass.new( "aarg1" , "arg2" ) { puts "my block" }


I would like BaseClass to receive the block I passed to the SubBaseClass
constructor. I have tried a few variations, but nothing went.

It involves '&'s:

class BaseClass
def initialize(arg1)
puts "got a block" if block_given?
yield
end
end

class SubBaseClass < BaseClass
def initialize(arg1, arg2, &block)
super(arg1) &block
end
end

sbc = SubBaseClass.new( "aarg1" , "arg2" ) { puts "my block" }


David
 
Z

Zach Dennis

Thanks a million!

Zach


Hi --




It involves '&'s:

class BaseClass
def initialize(arg1)
puts "got a block" if block_given?
yield
end
end

class SubBaseClass < BaseClass
def initialize(arg1, arg2, &block)
super(arg1) &block
end
end

sbc = SubBaseClass.new( "aarg1" , "arg2" ) { puts "my block" }


David
 
N

nobu.nokada

Hi,

At Tue, 16 Nov 2004 12:13:51 +0900,
Zach Dennis wrote in [ruby-talk:120510]:
I am curious how I can capture a block that was passed. Here's what I'm
trying to do:

class BaseClass
def initialize( arg1 )
puts "got a block" if block_given?
end
end

class SubBaseClass < BaseClass
def initialize( arg1 , arg2 )
super( arg1 )
end
end

sbc = SubBaseClass.new( "aarg1" , "arg2" ) { puts "my block" }


I would like BaseClass to receive the block I passed to the SubBaseClass
constructor. I have tried a few variations, but nothing went.

Your code prints "got a block". "super" passes the given block
to the super class'es method, unless called with &nil.

# Please stop posting by replying to irrelevant threads.
# Otherwise I sometimes tend to miss new threads you posted.
 
R

Robert Klemme

Florian Gross said:
Objection to the objection: The singleton pattern is not widely used in
Ruby. Couldn't we rename the implementation of it? Maybe something like
"SingleInstanceClass"?

But this will have more impact on existing code. I vote for conservative
behavior here: leave Singleton as it is and invent a good name for the
method that returns TCFKASC (who will guess this? Ruby quiz #13.5... :))

Kind regards

robert
 
D

David A. Black

But this will have more impact on existing code. I vote for conservative
behavior here: leave Singleton as it is and invent a good name for the
method that returns TCFKASC (who will guess this? Ruby quiz #13.5... :))

The Class Formerly Known As Singleton Class. What do I win? :)


David
 
D

David A. Black

Hi --

Thanks a million!

See Nobu's response, though -- your code already worked :) But the
&-technique for capturing blocks is worth knowing about too.


David
 
R

Robert Klemme

David A. Black said:
:))

The Class Formerly Known As Singleton Class. What do I win? :)

Ooops, that was too fast. :) Must - make - up - prize...

You win a virtual pad on the shoulder combined with a virtual fanfare:
Tataaaaaa!

:)

robert
 
P

Paul Brannan

In message "Re: Kernel#singleton_class"

|| I'd call them singleton classes. Any objection?

|Yes, cause of singleton pattern.

Hmm, any other alternative?

I think I've heard them called "metaclasses"; is that the same thing or
just a bad application of the word metaclass?

I think in an ideal world (one which revolves around me) I would
continue to call them singleton classes, and completely eradicate the
singleton antipattern from the standard library.

Paul
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top