[HELP] : ruby and the class variables

Discussion in 'Ruby' started by smayemba, Jan 17, 2004.

  1. smayemba

    smayemba Guest

    Hi all,

    how can i use the class variables in ruby like in Java :

    class A
    {
    public static foo;
    }

    class B
    {
    public static bar = A.foo; //A.foo is what i am looking for to do.
    }

    Thanks for all

    Sébastien
    DESS Informatique des Systèmes Autonomes
    Université Paris 8, France
     
    smayemba, Jan 17, 2004
    #1
    1. Advertising

  2. il Sat, 17 Jan 2004 23:10:00 +0100, "smayemba" <> ha
    scritto::

    >Hi all,
    >
    >how can i use the class variables in ruby like in Java :


    beware, someone may flame you, this are static variables not class
    ones.

    >class A
    >{
    > public static foo;
    >}
    >
    >class B
    >{
    > public static bar = A.foo; //A.foo is what i am looking for to do.
    >}
    >
    >Thanks for all


    ruby has class variables, like this:
    >> class A
    >> @@v=10
    >> def self.v
    >> @@v
    >> end
    >> end

    => nil
    >> A.v

    => 10

    but these are shared with subclasses. You may use instance variables
    of Class objects like this:

    >> class A
    >> class << self
    >> attr_accessor :v
    >> end
    >> end

    => nil
    >> A.v=20

    => 20
    >> A.v

    => 20
     
    gabriele renzi, Jan 18, 2004
    #2
    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. Replies:
    10
    Views:
    35,915
    jporter892
    Jun 6, 2011
  2. E11
    Replies:
    1
    Views:
    4,801
    Thomas Weidenfeller
    Oct 12, 2005
  3. Replies:
    9
    Views:
    973
  4. Eric D.
    Replies:
    3
    Views:
    181
    Jeremy Henty
    Feb 1, 2006
  5. Ralph Shnelvar
    Replies:
    29
    Views:
    786
    David Masover
    Nov 30, 2009
Loading...

Share This Page