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

2
  • 1
    Here'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. Commented Aug 15, 2011 at 10:12
  • year this is what i am looking for! but @fl00r was the first who right it ^___^ Commented Aug 15, 2011 at 10:24

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

4 Comments

write it! what the problem? :) Because there is no any static_accessor by default. And stay cool, man
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.
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
It's not a syntax trick. It's using Ruby object model the way it's supposed to be used.
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.

8 Comments

I am taking about static variables + attr_accessor. it doesn't work it is a fact. Please dont paste parts of accessors faq
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.
why such smart ruby can't resolve difference between static and usual variable? why there are no static_accessor?
can you provide alternative solution with shorter and more pretty code?
nobody needs static_accessor and it is very easy to write it yourself.
|
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.