Here is my ruby code
class Demo
attr_accessor :lines
def initialize(lines)
self.lines = lines
end
end
In the above code I could have
I think there is a little difference that has not be mentioned yet. The instance variable @lines can only be accessed within the class.
class Foo
def initialize
@lines = 1
end
end
foo = Foo.new
foo.lines
>> NoMethodError: undefined method `lines' for #
foo.send(:lines)
>> 1
If you define attr_accessor :lines all instances of that class can access the lines variable:
class Foo
attr_accessor :lines
def initialize
self.lines = 1
end
end
foo = Foo.new
foo.lines
>> 1
If you want your variable to be accessible for all instances you should use attr_accessor.