From: "Jan E." Date: 2012-10-03T03:12:07+09:00 Subject: Re: still learning by doing - connecting rooms in a game Sebastjan H. wrote in post #1078397: > Sorry if I confused anyone. It's just that I didn't want any duplication > as Peter suggested, but then If I understand correctly I need either the > writer or accessor to be able to change the values of instance > variables. No, you only need to define setter methods (i. e. methods ending with a "="). class A def initialize @x = 4 end # getter for @x def x @x end # setter for @x def x=(value) @x = value end end a = A.new puts a.x # call getter a.x = 3 # call setter; the same as a.x=(3) puts a.x That's exactly what attr_accessor does: It defines getter and setter methods for the given variables. You could actually define it yourself: class Module def my_attr_accessor *vars vars.each do |var| # getter define_method(var) {instance_variable_get "@#{var}"} # setter define_method("#{var}=") {|val| instance_variable_set "@#{var}", val} end end end class A my_attr_accessor :x, :y def initialize @x = 2 end end a = A.new puts a.x a.x = 12 puts a.x > What I meant was, that if omit the attr_accessor completely and only > have methods for instance variables defined, then > > player.hp -=20 does nothing. You should actually get an error saying that player doesn't have a "hp=" method. -- Posted via http://www.ruby-forum.com/.