ruby access static variable
Solution 1
You can't do what you want to do :)
@harald is right. attr_reader
will define GETTER only for instance variable, for "static" (aka "class variables") you need to define setter and getter by yourself:
class A
@@ololo = 1
# instance level
# getter
def ololo
@@ololo
end
# setter
def ololo=trololo
@@ololo = trololo
end
# and class level
# if you need it
# getter
def self.ololo
@@ololo
end
# setter
def self.ololo=trololo
@@ololo = trololo
end
end
So:
a = A.new
b = A.new
A.ololo
#=> 1
a.ololo
#=> 1
A.ololo = 100
A.ololo
#=> 100
a.ololo
#=> 100
b.ololo
#=> 100
a.ololo = 4
A.ololo
#=> 4
...
Shorter one:
class A
@ololo = 1
class << self
attr_accessor :ololo
end
end
Solution 2
attr_accessor :ololo
defines the methods ololo
and ololo=
which work against an instance variable named @ololo. So what happens when you try to access A::ololo
ruby will find your instance method ololo
and fail since you're trying to call it as a class method.
Solution 3
Yes, you can.
class A
cattr_accessor :ololo
@@ololo = 1
end
class B
A.ololo #Gets class variable "ololo" from class A
end
This is basically a Ruby on Rails feature. However, outside Rails, you can obtain the functionality from the Ruby Facets gem:
https://github.com/rubyworks/facets/blob/master/lib/core-uncommon/facets/module/cattr.rb
See this discussion: cattr_accessor outside of rails
puchu
Updated on August 11, 2022Comments
-
puchu almost 2 years
class A @@ololo = 1 end A::ololo A.new.ololo NoMethodError: undefined method `ololo'
okey. I need an attr_reader
class B @@ololo = 1 attr_reader :ololo end A::ololo NoMethodError: undefined method `ololo' A.new.ololo => nil
wtf? is there any limit for ruby accessors?
class C @@ololo = 1 def self.ololo @@ololo end def ololo @@ololo end end C::ololo => 1 C.new.ololo => 1
Ruby men usually say "yeah! pretty good!". is this pretty good? Can anyone provide shorter code?
-
puchu almost 13 yearsI am taking about static variables + attr_accessor. it doesn't work it is a fact. Please dont paste parts of accessors faq
-
sepp2k almost 13 years
attr_accessor :ololo
defines an instance variable named@ololo
. No, it doesn't. Instance variables start existing the first time you set them - they aren't defined anywhere. -
puchu almost 13 yearswhy such smart ruby can't resolve difference between static and usual variable? why there are no static_accessor?
-
puchu almost 13 yearscan you provide alternative solution with shorter and more pretty code?
-
fl00r almost 13 yearsnobody needs
static_accessor
and it is very easy to write it yourself. -
fl00r almost 13 yearswrite it! what the problem? :) Because there is no any
static_accessor
by default. And stay cool, man -
fl00r almost 13 yearsit is not a piece of FAQ, man. I've just thought you didn't understand this Ruby idiom. That's it. If this is so obviouse for you - that's great.
-
puchu almost 13 yearsmy question was "is static_accessor exists? or is attr_accessor can resolve difference between usual variable and static". can you post your comment as an answer?
-
puchu almost 13 yearsthe answer was nobody needs static_accessor and it is very easy to write it yourself. your code is the same as i do in the question
-
Mchl almost 13 yearsThere's no
static
in Ruby. Class methods and variables are just instance methods and variables ofClass
object -
Mchl almost 13 yearsIt's not a syntax trick. It's using Ruby object model the way it's supposed to be used.
-
harald almost 13 years@sepp2k, you're right. I was a bit imprecise there, thanks for the correction.