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
On Jan 24, 2008, at 10:23 AM, Ronald F. wrote:
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 G. II
On Jan 24, 2008 11:23 AM, Ronald F. [email protected] wrote:
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.
James G. wrote:
On Jan 24, 2008, at 10:23 AM, Ronald F. 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