The utility of attr_writer

● ARCHIVED · READ-ONLY
Started by nio kasgami 6 posts View original ↗
  1. 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!
  2. 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
  3. Another Fen said:
    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...
  4. 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.
  5. whitesphere said:
    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 ...
  6. nio kasgami said:
    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. =)