The utility of attr_writer

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
I check the attr_ topic but

i am a little lost when is speak about the attr_writer...

I know attr_accessor permit to change data in game

and 

attr_reader serve for force a reading of a specific data

but I don't know what is the use of attr_writer

I hope people will can help me :3!
 

Another Fen

Veteran
Veteran
Joined
Jan 23, 2013
Messages
564
Reaction score
275
First Language
German
Primarily Uses
For short:

attr_reader :value creates a method to read an attributes value, attr_writer :value creates a method to change an attributes value and attr_accessor :value creates both of these methods.

I usually use attr_writer instead of attr_accessor when I want to redefine the corresponding read-method anyway, for example to lazily initialize an attribute:

Code:
attr_writer :value def value  return defined?(@value) ? @value : 0end
 
Last edited by a moderator:

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
For short:

attr_reader :value creates a method to read an attributes value, attr_writer :value creates a method to change an attributes value and attr_accessor :value creates both of these methods.
Ho so easy I never knew attr_accessor do both...
 

whitesphere

Veteran
Veteran
Joined
Mar 14, 2014
Messages
1,688
Reaction score
784
First Language
English
You would use attr_writer if you had an internal class member you wanted outside users only to modify, but not read. 

I rarely use it, myself.  Most often, I use attr_accessor or attr_reader.

Basically, attr_reader creates a function like this:

def var_name

  var_name

end

Attr_writer creates a function like this (I may have this syntax wrong):

def var_name=(new_value)

@var_name=new_value

end

Attr_accessor creates both functions.
 

nio kasgami

VampCat
Veteran
Joined
May 21, 2013
Messages
8,949
Reaction score
3,042
First Language
French
Primarily Uses
RMMV
You would use attr_writer if you had an internal class member you wanted outside users only to modify, but not read. 

I rarely use it, myself.  Most often, I use attr_accessor or attr_reader.

Basically, attr_reader creates a function like this:

def var_name

  var_name

end

Attr_writer creates a function like this (I may have this syntax wrong):

def var_name=(new_value)

@var_name=new_value

end

Attr_accessor creates both functions.
Ho thanks this is more clear so the attr_writer don't have really ''use'' in it I never saw someone using it ...
 

BoluBolu

Veteran
Veteran
Joined
Apr 24, 2014
Messages
452
Reaction score
117
Primarily Uses
Ho thanks this is more clear so the attr_writer don't have really ''use'' in it I never saw someone using it ...
Yeah in custom script RGSS3, why not do attr_accessor instead of attr_writer, it's your custom script so you should have full authority about all things inside your script. =)
 

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

Latest Threads

Latest Posts

Latest Profile Posts

Couple hours of work. Might use in my game as a secret find or something. Not sure. Fancy though no? :D
Holy stink, where have I been? Well, I started my temporary job this week. So less time to spend on game design... :(
Cartoonier cloud cover that better fits the art style, as well as (slightly) improved blending/fading... fading clouds when there are larger patterns is still somewhat abrupt for some reason.
Do you Find Tilesetting or Looking for Tilesets/Plugins more fun? Personally I like making my tileset for my Game (Cretaceous Park TM) xD
How many parameters is 'too many'??

Forum statistics

Threads
105,862
Messages
1,017,047
Members
137,569
Latest member
Shtelsky
Top