Everything works as expected, save fetch_one(). Why does it not return "James"?
The problem is that the class variables are defined for Module, and not for the class Accessor:
class Module
def cattr_accessor( *symbols )
symbols.each do |sym|
eval "def self.#{sym}() @@#{sym} end
def self.#{sym}=(value) @@#{sym} = value end"
end
end
end
class A
cattr_accessor :one
end
class B
cattr_accessor :one
end
A.one = 1; p A.one # prints 1
B.one = 42; p A.one # prints 42!
I was playing around with an idea in another thread and ran into a surprise. Can anyone explain the following to me?
irb(main):015:0> class Module
irb(main):016:1> def cattr_accessor( *symbols )
irb(main):017:2> symbols.each do |sym|
irb(main):018:3* eval "def self.#{sym}( ) @@#{sym} end
irb(main):019:3" def self.#{sym}=( value ) @@#{sym} = value end"
irb(main):020:3> end
irb(main):021:2> end
irb(main):022:1> end
Do you really need to use @@variables? From my experience they tend to cause trouble.
Do you really need to use @@variables? From my experience they tend to cause trouble.
Could you explain why they would cause trouble?
I thought the original post would answer that well enough already, but in my own Ruby experience I have so far being surprised when using them. Why should they be used when you can use far simpler constructs with clearer rules?
Generally they seem to be used most as a quick replacement for instance variables when something goes wrong without further questioning or actually finding the root of the problem. I've seen them used quite frequently in this case:
module StackMixIn
def push(item) @items << item
end
def pop() @items.pop
end
# I'd like to initialize @items, but this will not work: @items =
# If I just replace every '@items' with '@@items' it will magically
# work. It's an obvious fix. It must be in Ruby exactly as a fix for
# this kind of situation.
end
(And yes, I know that they are going to be simplified in Rite, but I'm not sure if that is going to make them completely non-surprising to most people...)
I thought the original post would answer that well enough already, but
Well, someone clarified what the problem was... he was assigning to
the class variables for Module instead of for the class itself. I
guess "self" is what is confusing.
in my own Ruby experience I have so far being surprised when using them.
Why should they be used when you can use far simpler constructs with
clearer rules?
Well, the idea is to have shared storage for all objects of a
particular class, right? Class variables fit exactly that
requirement. I'm not sure which simpler constructs you're referring
to in this context... but perhaps I'm still too much in the Java
mindset.
I thought the original post would answer that well enough already, but
Well, someone clarified what the problem was... he was assigning to
the class variables for Module instead of for the class itself. I
guess "self" is what is confusing.
in my own Ruby experience I have so far being surprised when using them.
Why should they be used when you can use far simpler constructs with
clearer rules?
Well, the idea is to have shared storage for all objects of a
particular class, right? Class variables fit exactly that
requirement.
As long as you add "... and possibly objects of another class"
It's that inheritance thing, at least in large part, that causes
people to find class variables awkward.