Forum: Ruby on Rails Java Like public constant ?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Kevin Yang (Guest)
on 2007-06-08 13:26
(Received via mailing list)
Hi ,

    Some times there is a need to to store some constant variables in
a Ruby Class.

   we can do that in Java like :
   Public Static Class A{
       public static int LUCKY_NUMBER=100;
   }

   then we can use it everywhere A.LUCKY_NUMBER


   was wondering how can I do the same thing in Ruby?


thanks
Kevin
Douglas S. (Guest)
on 2007-06-08 16:05
Kevin Yang wrote:

>    was wondering how can I do the same thing in Ruby?


class Foo

  BAR = 12345

  def bar
    BAR
  end

end

> Foo::BAR
=> 12345

# or

> Foo.bar
=> 12345

The bar method is only a convenience, and ca be discarded if you don't
mind the :: notation.
This topic is locked and can not be replied to.