Is self.var in a class the same as @@var?

Is this the same?

class Test
@@class_var= “”
end

class Test
self.clas_var= “”
end

Thanks

On 6/14/07, Emmanuel O. [email protected] wrote:

Thanks


Posted via http://www.ruby-forum.com/.

No it is not, as a matter of fact the second idiom is not correct
(even with the typo corrected)…
irb(main):001:0> class Test
irb(main):002:1> @@class_var= “”
irb(main):003:1> end
=> “”
irb(main):004:0>
irb(main):005:0* class Test
irb(main):006:1> self.clas_var= “”
irb(main):007:1> end
NoMethodError: undefined method `clas_var=’ for Test:Class
from (irb):6
from :0
irb(main):008:0>


I believe that you are looking for accessors to class instance
variables, there are no accessors for class variables

class Test
@a = 42 ### Class instance variable
@@b = 42 ### Class variable
class << self
attr_accessor :a, :b
def show
puts “civar b #{self.b}”
puts “cvar b#{@@b}”
end
end
end

puts Test.a
puts Test.b
Test.b=1
puts Test.b
Test.show


This is a frequent source of confusion at first, so pls keep asking if
I was not very clear :slight_smile:

HTH
Robert

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs