injecting dynamic methods into a class

J

jonathan

jonathan said:
that each method is static (doesn't change any data members). One could

Oops, meant to say 'doesn't change any non-static (or non-class-wide, or
instance-specific)' members.

--Jonathan
 
J

jonathan

logancapaldo said:
And the slighlty harder way

class IAlsoAmASingleton
class << self
private :new
end
def self.instance
@inst ||= new
end
end

Ahh. That makes sense.

Thanks!
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: injecting dynamic methods into a class"
on Thu, 8 Dec 2005 11:45:18 +0900,

|> |How do you all think the term 'static class' fits?
|>
|> Nope, just because it's not static at all.

|It seems to be a term that .NET (and I think C++) uses:
|
|see <a
|href="http://msdn2.microsoft.com/en-us/library/79b3xss3.aspx">http://msdn2.microsoft.com/en-us/library/79b3xss3.aspx</a>

I thought we were talking about so called singleton classes, right?
It's totally different from static classes in C# and C++.
Singleton classes in Ruby are:

* created run time on demand
* can be modified (or enhanced) run time
* their methods may modify any instances
* their scope are not limited to certain file

matz.
 
G

gwtmp01

Having many terms for the same thing isn't necessarily a bad
thing. It
happens in every other aspect of programming as well (class methods
are
called 'member functions', 'methods', 'member methods', etc.).

Yes but we don't have Class#member, Class#method, Class#member_method,
Class#attribute, and Class#feature, etc. in Ruby. We have
Class#method and
that all by itself probably forces a canonical term for the concept.

I would guess that if there was no programatic way in Ruby to
reference or manipulate a singleton class then there wouldn't be such
a buzz about coming up with a standard name for it.

We have Object#class and Class#superclass which emphasize, if
not define, the canonical names for these things in Ruby. There is no
similar standard method in Ruby that returns the object generated by
the expression:

(class <<obj; self; end)

and since it is supremely awkward to type or say that expression to
refer to the concept, we all have our own habits in this regard and
only the social forces of the community to influence these habits.
I bet if you searched all the Ruby code ever written you would see
all sorts of things like:

def vclass(obj)
(class <<obj; self; end)
end

where vclass might be: meta, singleton, sclass, virtclass, eclass,
eigen,
aclass, adhoc, shadow, and so on. Everybody is coming up with their own
mnemonic for the concept because, well, you need a name if you are going
wrap up that expression in a method.

So all this is a long way of saying that what I really want for
Christmas
is a short name for (class <<obj; self; end). But if I don't get that,
I'll still be very happy with getting Ruby 1.8.4, which I will brag
about
to all my friends who are still stuck with their crufty old static
language
tools. :)

Gary Wright
 
D

dblack

Hi --

So all this is a long way of saying that what I really want for Christmas
is a short name for (class <<obj; self; end). But if I don't get that,
I'll still be very happy with getting Ruby 1.8.4, which I will brag about
to all my friends who are still stuck with their crufty old static language
tools. :)

I'm hoping my Kernel#singleton_class RCR will get approved in some
form once some of this is resolved. It's partly a question of whether
the implementation of per-object behavior is going to continue to be
as class-based as it is now.


David
 
J

jonathan

matz said:
I thought we were talking about so called singleton classes, right?
It's totally different from static classes in C# and C++.

Well, I thought we were talking about classes which contain only class
methods and class data (which are currently called 'singletons'). But,
in C# or C++ these are called 'static classes.' I guess that makes
sense for them because the code itself can never change or be enhanced
(like it can be in Ruby).
Singleton classes in Ruby are:

* created run time on demand
* can be modified (or enhanced) run time
* their methods may modify any instances
* their scope are not limited to certain file

matz.

Ok. That's what I meant by metaprogramming. I wasn't aware of the
scope issue though. Also, what do you mean by 'their methods may modify
any instances'?

But, yea, given the 'dynamic' nature of Ruby, it doesn't make sense to
say something is static.

--Jonathan
 
J

jonathan

gwtmp01 said:
Yes but we don't have Class#member, Class#method, Class#member_method,
Class#attribute, and Class#feature, etc. in Ruby. We have
Class#method and
that all by itself probably forces a canonical term for the concept.

Ahh. Ok. I think I also gathered from other posts that it also helps
to be able to differentiate these for the purpose of error messages.
Well, this may be a solution:

Let the type of singleton which implements the singleton design pattern
remain as a 'singleton' in error messaging and introspection. But, let
the class which is a singleton by having nothing but class methods be
known as a simpleton.

In essence, from the outside, they look the same (and please correct me
if I'm wrong because I don't know all the Ruby syntax yet, but in
essence I think they are the same). Simpleton and singleton are
synonyms as far as I know. And, these two ways of arriving at a
singleton produce the same effect for the consumer (but differentiating
them is nice for error messaging and introspection).

--Jonathan
 
Y

Yukihiro Matsumoto

Hi,

In message "Re: injecting dynamic methods into a class"

|> Singleton classes in Ruby are:
|>
|> * created run time on demand
|> * can be modified (or enhanced) run time
|> * their methods may modify any instances
|> * their scope are not limited to certain file
|>
|> matz.
|
|Ok. That's what I meant by metaprogramming. I wasn't aware of the
|scope issue though. Also, what do you mean by 'their methods may modify
|any instances'?

Nevermind. I was confusing const and static. Silly.

matz.
 
D

dblack

Hi --

Ahh. Ok. I think I also gathered from other posts that it also helps
to be able to differentiate these for the purpose of error messages.
Well, this may be a solution:

Let the type of singleton which implements the singleton design pattern
remain as a 'singleton' in error messaging and introspection. But, let
the class which is a singleton by having nothing but class methods be
known as a simpleton.

Do you know what the word "simpleton" means? It's really not a
candidate for a name for a language construct.

Anyway -- I'm trying to follow along but not sure what you mean by
classes that have nothing but class methods and class data. Can you
give a code example of such a class?


David
 
J

jonathan

dblack said:
Anyway -- I'm trying to follow along but not sure what you mean by
classes that have nothing but class methods and class data. Can you
give a code example of such a class?

Here's a very simple example (a global counter):

<code>

class SingletonCounter
private_class_method :new
attr_accessor :count
@@counter = nil # class var which pts to instance
def initialize( count )
@count = count
end
def SingletonCounter.create
@@counter = new( 0 ) unless @@counter
@@counter
end
def increment( inc_amt )
@count += inc_amt
return @count
end
end

class Counter
@@count = 0 # class variable
def Counter.increment( inc_amt ) # class method
@@count = @@count + inc_amt
return @@count
end
end

sc = SingletonCounter.create
print sc.increment( 9 )
print sc.increment( 4 )

sc2 = SingletonCounter.create
print sc2.increment( 3 ) # points to same underlying obj as sc

c = Counter.new
print Counter.increment( 9 ) # in C#, you could do c.increment( 9 )
here
print Counter.increment( 4 )

c2 = Counter.new
print Counter.increment( 3 )

</code>

The first class is a true singleton and the second class is what is *in
essence* a singleton, but known as a static class in C#. One thing that
C# will let you do that makes this more transparent is
instancevar.staticmethod(). That apparently isn't the case in Ruby, so
that makes them less equivalent. (I suspected there would end up being
some syntax difference between the two, but the point I was making was
that they were the same in concept).

I don't know if any of this is really relevant, but hopefully you
understand statics, singletons, and C# a little better now. :) BTW,
this discussion is continued in another thread called 'About class
methods'.

--J
 

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,769
Messages
2,569,582
Members
45,066
Latest member
VytoKetoReviews

Latest Threads

Top