Atribute and @variables. what's the difference?

Started by Valdred, April 12, 2010, 11:22:36 am

Previous topic - Next topic

Valdred

Yeah, what is the difference between attributes and variables?
I don't get it.

Blizzard

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.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Fantasist

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

Do you like ambient/electronic music? Then you should promote a talented artist! Help out here. (I'm serious. Just listen to his work at least!)


The best of freeware reviews: Gizmo's Freeware Reviews




Zeriab

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.

Valdred

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?

Blizzard

@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.
Check out Daygames and our games:

King of Booze 2      King of Booze: Never Ever
Drinking Game for Android      Never have I ever for Android
Drinking Game for iOS      Never have I ever for iOS


Quote from: winkioI do not speak to bricks, either as individuals or in wall form.

Quote from: Barney StinsonWhen I get sad, I stop being sad and be awesome instead. True story.

Valdred

I see, thank you very much for your help. *levels up*

Zeriab

: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

Valdred