Chaos Project

RPG Maker => RPG Maker Scripts => Script Troubleshooting => Topic started by: Valdred on April 12, 2010, 11:22:36 am

Title: Atribute and @variables. what's the difference?
Post by: Valdred on April 12, 2010, 11:22:36 am
Yeah, what is the difference between attributes and variables?
I don't get it.
Title: Re: Atribute and @variables. what's the difference?
Post by: Blizzard on April 13, 2010, 03:39:59 am
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.
Title: Re: Atribute and @variables. what's the difference?
Post by: Fantasist on April 13, 2010, 06:47:40 am
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

Title: Re: Atribute and @variables. what's the difference?
Post by: Zeriab on April 13, 2010, 08:47:29 am
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.
Title: Re: Atribute and @variables. what's the difference?
Post by: Valdred on April 13, 2010, 08:51:05 am
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?
Title: Re: Atribute and @variables. what's the difference?
Post by: Blizzard on April 13, 2010, 08:55:19 am
@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.
Title: Re: Atribute and @variables. what's the difference?
Post by: Valdred on April 13, 2010, 10:09:20 am
I see, thank you very much for your help. *levels up*
Title: Re: Atribute and @variables. what's the difference?
Post by: Zeriab on April 15, 2010, 06:20:48 am
: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
Title: Re: Atribute and @variables. what's the difference?
Post by: Valdred on April 15, 2010, 10:23:10 am
ah, I see. Thank you.

*levels up & hugs*