problem about class-variables-Collection of common programming errors
sawa
ruby class-variables
Setting a class variable @@foo in two classes B and C, where neither is a subclass of the other but they both include a common module A, seems to create @@foo separately for B and C, which A cannot access:module A; end class B; include A; @@foo = 1 end class C; include A; @@foo = 2 endmodule A; p @@foo end # => NameError: uninitialized class variable @@foo in A class B; p @@foo end # => 1 class C; p @@foo end # => 2But when @@foo is assigned in A, which works as an ancestor to both B
aabes
ruby-on-rails class-variables
I’m a bit miffed why this:cattr_accessor :aggregate { true }fails with the error: syntax error, unexpected ‘{‘, expecting keyword_endcattr_accessor :aggregate { true }^while exactly the same thing, with do/end seems to be doing the right thing:cattr_accessor :aggregate do true end(but is whhhay too verbose ;)The source from :http://api.rubyonrails.org/classes/Class.html#method-i-cattr_writer is a bit too crufty given the time I have to spend on this. Thoughts?
Rafa
ruby inheritance class-variables
Like the following code shows, having class accessors defined in the superclass could have unexpected behaviours because the class accessor is the same variable for all the subclasses.class Super cattr_accessor :name endclass SubA < Super; end class SubB < Super; endSubA.name = “A” SubB.name = “B”SubA.name=> “B” # unexpected!I want to have an independent class accessor for each subclass, so a possible solution is moving the cattr_accessor from the superclass and putting it in every subc
Andrew Grimm
ruby instance-variables local-variables class-variables
Same in Ruby 1.8.7 and 1.9.2:$ irbruby-1.8.7-p302 > foo.nil? NameError: undefined local variable or method `foo’ for #<Object:0x3794c>from (irb):1ruby-1.8.7-p302 > @bar.nil?=> true ruby-1.8.7-p302 > @@wah.nil? NameError: uninitialized class variable @@wah in Objectfrom (irb):3why the instance variable treated differently than a local and class variable?
Web site is in building
I discovery a place to host code、demo、 blog and websites.
Site access is fast but not money