Injecting dynamic methods into a class

jonathan [email protected] <[email protected]. wrote:

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

Hi,

In message “Re: injecting dynamic methods into a class”
on Thu, 8 Dec 2005 11:45:18 +0900,
“jonathan [email protected] [email protected]
[email protected] writes:

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

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.
    

logancapaldo wrote:

And the slighlty harder way

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

Ahh. That makes sense.

Thanks!

On Dec 7, 2005, at 9:22 PM, jonathan wrote:

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. :slight_smile:

Gary W.

Hi –

On Thu, 8 Dec 2005, [email protected] wrote:

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. :slight_smile:

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


David A. Black
[email protected]

“Ruby for Rails”, forthcoming from Manning Publications, April 2006!

gwtmp01 wrote:

On Dec 7, 2005, at 9:22 PM, jonathan wrote:

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.

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

Hi,

In message “Re: injecting dynamic methods into a class”
on Thu, 8 Dec 2005 15:39:06 +0900, “jonathan [email protected]
[email protected] writes:

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

matz wrote:

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

Hi –

On Thu, 8 Dec 2005, jonathan [email protected]
[email protected] wrote:

Class#method and
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


David A. Black
[email protected]

“Ruby for Rails”, forthcoming from Manning Publications, April 2006!

dblack wrote:

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

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 )

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. :slight_smile: BTW,
this discussion is continued in another thread called ‘About class
methods’.

–J

Hi –

On Sat, 10 Dec 2005, jonathan [email protected]
[email protected] wrote:

class SingletonCounter
def increment( inc_amt )
end
print Counter.increment( 9 ) # in C#, you could do c.increment( 9 )
here
print Counter.increment( 4 )

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

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

In Ruby it’s just known as a class :slight_smile: I’d highly recommend not
trying to shoehorn the term “static” into Ruby. It’s never going to
be a good fit.

(Don’t neglect the Singleton module, by the way, which will do the
singleton-ing for you.)

There’s nothing at the language level that stops you from adding an
instance method to Counter. You can give a name to a style of coding
where you don’t write instance methods, but it’s definitely not a
language feature.

David


David A. Black
[email protected]

“Ruby for Rails”, forthcoming from Manning Publications, April 2006!

dblack wrote:

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?

Please Ignore.

[**1] Meatball Wiki: SockPuppet