[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

"Parametric class" on inheritance

Ronald Fischer

1/24/2008 4:24:00 PM

I found in "Ruby Cookbook" (recipe 8.8) a syntactically
interesting construct, which I would like to understand.
Here is the excerpt from the coding example:

require 'delegate'
class OrdinalNumber < DelegateClass(Fixnum)
...
end

I am a bit puzzled about the way the parent class is
written: DelegateClass(Fixnum). This looks like a
function call, only that DelegateClass is not a
function (since it starts with an upper-case letter).
How does it work out that DelegateClass(Fixnum) evaluates
to something of type "Class"?

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

3 Answers

James Gray

1/24/2008 4:30:00 PM

0

On Jan 24, 2008, at 10:23 AM, Ronald Fischer wrote:

> I found in "Ruby Cookbook" (recipe 8.8) a syntactically
> interesting construct, which I would like to understand.
> Here is the excerpt from the coding example:
>
> require 'delegate'
> class OrdinalNumber < DelegateClass(Fixnum)
> ...
> end
>
> I am a bit puzzled about the way the parent class is
> written: DelegateClass(Fixnum). This looks like a
> function call, only that DelegateClass is not a
> function (since it starts with an upper-case letter).
> How does it work out that DelegateClass(Fixnum) evaluates
> to something of type "Class"?

It is a method call actually. You are allowed to create method that
begin with a capital letter, though it's best only used for special
cases like this. Usually a capitalized method does some kind of
conversion, like what we see here or Kernel#Array, Kernel#String, etc.

James Edward Gray II

Brian Mitchell

1/24/2008 4:32:00 PM

0

On Jan 24, 2008 11:23 AM, Ronald Fischer <rofi@fusshuhn.de> wrote:
> I found in "Ruby Cookbook" (recipe 8.8) a syntactically
> interesting construct, which I would like to understand.
> Here is the excerpt from the coding example:
>
> require 'delegate'
> class OrdinalNumber < DelegateClass(Fixnum)
> ...
> end
>
> I am a bit puzzled about the way the parent class is
> written: DelegateClass(Fixnum). This looks like a
> function call, only that DelegateClass is not a
> function (since it starts with an upper-case letter).
> How does it work out that DelegateClass(Fixnum) evaluates
> to something of type "Class"?

When in doubt check it out in irb:

>> require 'delegate'
=> true
>> DelegateClass(Fixnum)
=> #<Class:0x35dd9c>

So DelegateClass is indeed a function that returns a class and the <
syntax does allow expressions on the right hand side.

Brian.

Ronald Fischer

1/25/2008 7:54:00 AM

0

James Gray wrote:
> On Jan 24, 2008, at 10:23 AM, Ronald Fischer wrote:
>
>> How does it work out that DelegateClass(Fixnum) evaluates
>> to something of type "Class"?
>
> It is a method call actually. You are allowed to create method that
> begin with a capital letter, though it's best only used for special
> cases like this. Usually a capitalized method does some kind of
> conversion, like what we see here or Kernel#Array, Kernel#String, etc.

Thanks a lot (also to Brian) for the helpful explanation!

Ronald

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