Yeah, what is the difference between attributes and variables?
I don't get it.
Attributes are usually a term from the object oriented paradigm. An object has an attribute, i.e. My attribute would be that I have blue eyes. A variable is a concept from programming (it's also one from mathematics, but that's irrelevant here) and it is used to describe or "contain" a value that represents an attribute.
In ruby instance variables are also called attributes.
As for a more limited clarification, what you define as "attr_reader", "attr_accessor", "attr_writer" are simply instance variables (variables starting with a single "@") with methods to modify them from outside the object:
attr_reader :eye_color
#=== That equals ===
def eye_color
return @eye_color
end
attr_writer :eye_color
#=== That equals ===
def eye_color=(value)
@eye_color = value
end
attr_accessor :eye_color
#=== That equals ===
def eye_color
return @eye_color
end
def eye_color=(value)
@eye_color = value
end
I'll assume you mean the difference between instance variables (@var) and attribute constructors (attr_XXXX) in rgss.
In that case the attribute constructors are shortcuts for creating getters and settings of instance variables. In other words. shortcuts for for allowing instance variables to be seen and/or changed from outside the object.
Instance variables keep instance relevant data. I.e. to the specific object.
Thanks a lot guys. do @abc equal :abc? or did I misunderstand.
But one more thing.
What is the difference between attr_accessor and attr_reader?
@abc is the value of the variable while :abc is the variable object itself that contains the value.
attr_reader allows only read-only access of the variable object's value while attr_accessor allows writing (aka changing) the value as well. There's also attr_writer which allows only writing and no reading.
I see, thank you very much for your help. *levels up*
:abc is a symbol.
You can consider it like an immutable string where equal strings refer to the same object.
a = :abc
b = :abc
p a == b # => true
p a.id == b.id # => true
ah, I see. Thank you.
*levels up & hugs*