how to implement class constants?

Discussion in 'Ruby' started by ruud, Feb 18, 2008.

  1. ruud

    ruud Guest

    hi list,

    what construct do I have to use in Ruby to create class constants (so
    no instance necessary to use it)?

    thanks, Ruud
     
    ruud, Feb 18, 2008
    #1
    1. Advertising

  2. ruud

    7stud -- Guest

    ruud wrote:
    > hi list,
    >
    > what construct do I have to use in Ruby to create class constants (so
    > no instance necessary to use it)?
    >
    > thanks, Ruud


    This seems to work:

    class Dog
    Color = 'brown'

    def Dog.Color
    Color
    end

    end

    puts Dog.Color

    --
    Posted via http://www.ruby-forum.com/.
     
    7stud --, Feb 18, 2008
    #2
    1. Advertising

  3. ruud

    ThoML Guest

    > This seems to work:

    Or maybe:

    class Dog
    Color = 'brown'
    end

    puts Dog::Color

    Dog::Attrib = 'cute'

    puts Dog::Attrib
     
    ThoML, Feb 18, 2008
    #3
  4. ruud

    ruud Guest

    Thank you both for you answer. I like the Class::var solution best;
    the other one includes more typing.

    I am fairly new to Ruby so all the time I am occupied asking myself
    easy-to-answer questions. This really helps.

    Another easy to answer question is a bit related:
    Most of the time I find a class.var notation of a class::var notation.
    In documentation I find class#method notation. How are these three
    related to each other?

    thanks, Ruud
     
    ruud, Feb 18, 2008
    #4
  5. ruud wrote:
    > Most of the time I find a class.var notation of a class::var notation.


    That's not entirely accurate. There is no class.var. There's object.method
    (where object can be a class and the method can have the same name as a
    variable, but it still has to be a method) and there's namespace::Constant
    where namespace can be a class or a module and Constant has to be a constant
    (it can't be any other sort of variable).
    The :: is for accessing constants in a namespace and the . is for calling
    methods on an object.
    It is not possible to access non-constant variables from outside the class
    without using methods designed for that purpose.


    > In documentation I find class#method notation. How are these three
    > related to each other?


    class#method describes an instance method (as opposed to class.method which
    would describe a class method). If you see something like String#length, that
    means that you could e.g. write "hello".length, but not String.length. If you
    see String.new, you have to actually type String.new and not something
    like "hello".new.


    HTH,
    Sebastian
    --
    Jabber:
    ICQ: 205544826
     
    Sebastian Hungerecker, Feb 18, 2008
    #5
  6. On 18/02/2008, Sebastian Hungerecker <> wrote:

    Sebastian,
    it couldn't have been clearer. Thank you for your extensive reply!

    Ruud
     
    ruud grosmann, Feb 18, 2008
    #6
    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. TJS

    constants in code class

    TJS, Nov 5, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    311
  2. -
    Replies:
    6
    Views:
    445
    Wibble
    May 7, 2005
  3. E11
    Replies:
    1
    Views:
    4,844
    Thomas Weidenfeller
    Oct 12, 2005
  4. Vikas
    Replies:
    1
    Views:
    318
    Stephan Br?nnimann
    Sep 10, 2004
  5. Generic Usenet Account

    Declaring constants within the scope of a class

    Generic Usenet Account, Mar 15, 2005, in forum: C++
    Replies:
    5
    Views:
    3,597
    Howard
    Mar 15, 2005
Loading...

Share This Page