3
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?

P.M
  • 2,880
  • 3
  • 43
  • 53
puchu
  • 3,294
  • 6
  • 38
  • 62
  • 1
    [Here](https://gist.github.com/1145995)'s something that might help you along the right track. Its been too long since I've done anything to do with class variables and class-level instance variables to provide a proper answer though. – Nemo157 Aug 15 '11 at 10:12
  • year this is what i am looking for! but @fl00r was the first who right it ^___^ – puchu Aug 15 '11 at 10:24

3 Answers3

17

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
fl00r
  • 82,987
  • 33
  • 217
  • 237
  • write it! what the problem? :) Because there is no any `static_accessor` by default. And stay cool, man – fl00r Aug 15 '11 at 10:08
  • it 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. – fl00r Aug 15 '11 at 10:15
  • the 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 – puchu Aug 15 '11 at 10:17
  • It's not a syntax trick. It's using Ruby object model the way it's supposed to be used. – Mchl Aug 15 '11 at 10:32
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

Community
  • 1
  • 1
La-comadreja
  • 5,627
  • 11
  • 36
  • 64
3

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.

harald
  • 5,976
  • 1
  • 24
  • 41
  • I am taking about static variables + attr_accessor. it doesn't work it is a fact. Please dont paste parts of accessors faq – puchu Aug 15 '11 at 09:52
  • `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. – sepp2k Aug 15 '11 at 09:55
  • why such smart ruby can't resolve difference between static and usual variable? why there are no static_accessor? – puchu Aug 15 '11 at 10:00
  • can you provide alternative solution with shorter and more pretty code? – puchu Aug 15 '11 at 10:01
  • 1
    nobody needs `static_accessor` and it is very easy to write it yourself. – fl00r Aug 15 '11 at 10:06
  • my 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 Aug 15 '11 at 10:15
  • There's no `static` in Ruby. Class methods and variables are just instance methods and variables of `Class` object – Mchl Aug 15 '11 at 10:23
  • @sepp2k, you're right. I was a bit imprecise there, thanks for the correction. – harald Aug 15 '11 at 15:43