[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Re: injecting dynamic methods into a class

jonathan

12/8/2005 1:59:00 AM

transfire wrote:
>> 'Ad hoc' has too many negative connotations and singleton has a fairly
>> unambiguous meaning.
>
> I felt the same way at first, until I started using it, keeping in mind
> the strict definition --even the Latin definition: *for this*.
>

That's really interesting. It's unfortunate that the most common use of
the term is a secondary meaning. I guess using it will motivate people
to find out primary meaning. I'm all for it. :)



--
Posted via http://www.ruby-....


15 Answers

jonathan

12/8/2005 2:22:00 AM

0

jonathan wrote:
> transfire wrote:
>>> 'Ad hoc' has too many negative connotations and singleton has a fairly
>>> unambiguous meaning.
>>
>> I felt the same way at first, until I started using it, keeping in mind
>> the strict definition --even the Latin definition: *for this*.
>>
>
> That's really interesting. It's unfortunate that the most common use of
> the term is a secondary meaning. I guess using it will motivate people
> to find out primary meaning. I'm all for it. :)

Just to clarify (I hadn't finished reading all the posts when I made the
above comment). I don't mean to make my vote 'for' this term, but, I
meant that I was for using a term in a way that educates. Most of us
are people that like to learn anyway. :)

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

How do you all think the term 'static class' fits?

Also, one more thing:

How do you actually implement the singleton pattern in Ruby? Not being
able to return a self ptr from initialize seems to stump me.

--Jonathan

--
Posted via http://www.ruby-....


Logan Capaldo

12/8/2005 2:30:00 AM

0


On Dec 7, 2005, at 9:22 PM, jonathan <zjll9@imail.etsu.edu> wrote:

> jonathan wrote:
>> transfire wrote:
>>>> 'Ad hoc' has too many negative connotations and singleton has a
>>>> fairly
>>>> unambiguous meaning.
>>>
>>> I felt the same way at first, until I started using it, keeping
>>> in mind
>>> the strict definition --even the Latin definition: *for this*.
>>>
>>
>> That's really interesting. It's unfortunate that the most common
>> use of
>> the term is a secondary meaning. I guess using it will motivate
>> people
>> to find out primary meaning. I'm all for it. :)
>
> Just to clarify (I hadn't finished reading all the posts when I
> made the
> above comment). I don't mean to make my vote 'for' this term, but, I
> meant that I was for using a term in a way that educates. Most of us
> are people that like to learn anyway. :)
>
> 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.).
>
> How do you all think the term 'static class' fits?
>
> Also, one more thing:
>
> How do you actually implement the singleton pattern in Ruby? Not
> being
> able to return a self ptr from initialize seems to stump me.
>
> --Jonathan
>
> --
> Posted via http://www.ruby-....
>

Well the easy way is

require 'singleton'
class IWantToBeASingleton
include Singleton
end

And the slighlty harder way

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




Yukihiro Matsumoto

12/8/2005 2:30:00 AM

0

Hi,

In message "Re: injecting dynamic methods into a class"
on Thu, 8 Dec 2005 11:22:02 +0900, "jonathan <zjll9@imail.etsu.edu>" <zjll9@imail.etsu.edu> writes:

|How do you all think the term 'static class' fits?

Nope, just because it's not static at all.

|How do you actually implement the singleton pattern in Ruby? Not being
|able to return a self ptr from initialize seems to stump me.

Four ideas:

* A plain object + singleton methods
* A module with singleton methods
* singleton library in the standard libraries
* An ordinary class and pre-allocated object with convention not to
create any other objects.

matz.


jonathan

12/8/2005 2:45:00 AM

0

matz wrote:
> Hi,
>
> In message "Re: injecting dynamic methods into a class"
> on Thu, 8 Dec 2005 11:22:02 +0900, "jonathan <zjll9@imail.etsu.edu>"
> <zjll9@imail.etsu.edu> 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...

In what way would the class not be static in Ruby? In the sense that
you can still metaprogram (or is it more than that)?

I think the term static refers to the fact that the data is static (that
is, there isn't a new copy of the data members for each instance) and
that each method is static (doesn't change any data members). One could
argue that adding code to a class is dynamic, but it could be understood
that the term 'static' only refers to data.

I guess the term is not a *perfect* match. But, then again, none of the
ones suggested, except maybe 'eigenclass' is.

--Jonathan

--
Posted via http://www.ruby-....


jonathan

12/8/2005 2:48:00 AM

0

jonathan <zjll9@imail.etsu.edu> <zjll9@imail.etsu. 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

--
Posted via http://www.ruby-....


jonathan

12/8/2005 3:06:00 AM

0

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!

--
Posted via http://www.ruby-....


Yukihiro Matsumoto

12/8/2005 3:36:00 AM

0

Hi,

In message "Re: injecting dynamic methods into a class"
on Thu, 8 Dec 2005 11:45:18 +0900,
"jonathan <zjll9@imail.etsu.edu> <zjll9@imail.etsu.edu>" <zjll9@imail.etsu.edu> 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.


Gary Wright

12/8/2005 3:44:00 AM

0


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

Gary Wright


dblack

12/8/2005 4:00:00 AM

0

jonathan

12/8/2005 6:39:00 AM

0

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

--
Posted via http://www.ruby-....