accessing class constants in included modules

Discussion in 'Ruby' started by Martin DeMello, Nov 8, 2006.

  1. module Foo
    def display
    puts self.class::A
    end
    end

    class Bar
    A = 5
    include Foo
    end

    class Baz
    A = 6
    include Foo
    end

    Bar.new.display
    Baz.new.display


    Anything cleaner-looking than self.class::Constant to access the
    constant from a method defined in the module?

    martin
    Martin DeMello, Nov 8, 2006
    #1
    1. Advertising

  2. Martin DeMello

    Tom Agnew Guest

    Martin DeMello wrote:
    > module Foo
    > def display
    > puts self.class::A
    > end
    > end
    >
    > class Bar
    > A = 5
    > include Foo
    > end
    >
    > class Baz
    > A = 6
    > include Foo
    > end
    >
    > Bar.new.display
    > Baz.new.display
    >
    >
    > Anything cleaner-looking than self.class::Constant to access the
    > constant from a method defined in the module?
    >
    > martin
    >


    Here's a trick I use to beautify access to constants:

    class Object
    def my
    self.class
    end
    end

    .....Now all constants can be accessed with my::Constant.

    I find it more readable. Certainly less typing!

    Cheers,
    Tom Agnew
    Tom Agnew, Nov 9, 2006
    #2
    1. Advertising

  3. On 11/9/06, Tom Agnew <> wrote:

    > Here's a trick I use to beautify access to constants:
    >
    > class Object
    > def my
    > self.class
    > end
    > end
    >
    > ....Now all constants can be accessed with my::Constant.
    >
    > I find it more readable. Certainly less typing!


    Ah - nice!

    martin
    Martin DeMello, Nov 9, 2006
    #3
  4. Martin DeMello wrote:
    > On 11/9/06, Tom Agnew <> wrote:
    >
    >> Here's a trick I use to beautify access to constants:
    >>
    >> class Object
    >> def my
    >> self.class
    >> end
    >> end
    >>
    >> ....Now all constants can be accessed with my::Constant.
    >>
    >> I find it more readable. Certainly less typing!

    >
    > Ah - nice!


    This may be a corner case, but if you're going to define #my at the
    instance level, it should probably be based on the
    (singleton|eigen)class of the instance and not the "birth" class:

    class Object
    def my
    self.class
    #class << self; self; end
    end
    end

    class Foo
    C = 2
    end

    x = Foo.new
    class << x
    X = 3
    end

    p x.my::C
    p x.my::X

    Now try it with the alternative!

    --
    vjoel : Joel VanderWerf : path berkeley edu : 510 665 3407
    Joel VanderWerf, Nov 9, 2006
    #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. Ara.T.Howard
    Replies:
    3
    Views:
    123
    Robert Klemme
    Feb 16, 2004
  2. Daniel Berger
    Replies:
    3
    Views:
    109
    Mauricio Fernández
    Nov 22, 2005
  3. Dale
    Replies:
    1
    Views:
    113
  4. Chirag Mistry
    Replies:
    22
    Views:
    314
    dohzya
    Aug 1, 2007
  5. Mark Wilden
    Replies:
    5
    Views:
    188
    Rick DeNatale
    May 15, 2008
Loading...

Share This Page