"Parametric class" on inheritance

Discussion in 'Ruby' started by Ronald Fischer, Jan 24, 2008.

  1. 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-forum.com/.
     
    Ronald Fischer, Jan 24, 2008
    #1
    1. Advertising

  2. Ronald Fischer

    James Gray Guest

    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
     
    James Gray, Jan 24, 2008
    #2
    1. Advertising

  3. On Jan 24, 2008 11: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"?


    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.
     
    Brian Mitchell, Jan 24, 2008
    #3
  4. 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-forum.com/.
     
    Ronald Fischer, Jan 25, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. maxw_cc
    Replies:
    1
    Views:
    3,148
    Martijn van Steenbergen
    Dec 21, 2003
  2. cppsks
    Replies:
    0
    Views:
    827
    cppsks
    Oct 27, 2004
  3. karthikbalaguru
    Replies:
    9
    Views:
    1,041
  4. Daniel Pitts
    Replies:
    27
    Views:
    1,908
    Mike Schilling
    Feb 27, 2008
  5. johnsonlau
    Replies:
    1
    Views:
    777
    Kai-Uwe Bux
    Jul 21, 2008
Loading...

Share This Page