#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2007
    Posts
    1
    Rep Power
    0

    Class constants from modules


    I'd like to make a class constant using a module -- I cannot use < because I am extending unit_test. -- Here's the idea:

    irb(main):001:0> module One @@constant="constant" end
    => "constant"
    irb(main):002:0> module Two
    irb(main):003:1> def work
    irb(main):004:2> puts(@@constant)
    irb(main):005:2> end
    irb(main):006:1> end
    => nil
    irb(main):007:0> class Constant_work
    irb(main):008:1> include One
    irb(main):009:1> include Two
    irb(main):010:1> end
    => Constant_work
    irb(main):011:0> Constant_work.new.work
    NameError: uninitialized class variable @@constant in Two
    from (irb):4:in `work'
    from (irb):11
    irb(main):012:0>

    How can I get this to happen?

    Dale
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    How about:
    Code:
    irb(main):021:0> module One @@con = "Constant" end
    => "Constant"
    irb(main):022:0> module Two include One
    irb(main):023:1> def work
    irb(main):024:2> puts(@@con)
    irb(main):025:2> end
    irb(main):026:1> end
    => nil
    irb(main):027:0> class Constant_work
    irb(main):028:1> include Two
    irb(main):029:1> end
    => Constant_work
    irb(main):030:0> Constant_work.new.work
    Constant
    => nil
    irb(main):031:0>
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog

IMN logo majestic logo threadwatch logo seochat tools logo