"Parametric class" on inheritance

R

Ronald Fischer

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
 
J

James Gray

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
 
B

Brian Mitchell

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:
=> #<Class:0x35dd9c>

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

Brian.
 
R

Ronald Fischer

James said:
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
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top