K
Kedar Mhaswade
I observed that once I have defined attr_accessor for an attribute of a
class, I can access it with or without an '@' leading the name of
attribute (i.e. both [1] and [2] work correctly).
I find accessing it without leading '@' more natural (when there are no
conflicts with local variables).
What is the Ruby community convention in this regard?
Regards,
Kedar
[1]
#!/usr/bin/env ruby
class Car
attr_accessor :color
def initialize(color)
@color = color
end
def print_color
puts @color
end
end
c = Car.new("blue")
c.print_color
[2]
#!/usr/bin/env ruby
class Car
attr_accessor :color
def initialize(color)
@color = color
end
def print_color
puts color
end
end
c = Car.new("blue")
c.print_color
class, I can access it with or without an '@' leading the name of
attribute (i.e. both [1] and [2] work correctly).
I find accessing it without leading '@' more natural (when there are no
conflicts with local variables).
What is the Ruby community convention in this regard?
Regards,
Kedar
[1]
#!/usr/bin/env ruby
class Car
attr_accessor :color
def initialize(color)
@color = color
end
def print_color
puts @color
end
end
c = Car.new("blue")
c.print_color
[2]
#!/usr/bin/env ruby
class Car
attr_accessor :color
def initialize(color)
@color = color
end
def print_color
puts color
end
end
c = Car.new("blue")
c.print_color