From: Peter Vandenabeele Date: 2012-02-03T06:12:09+09:00 Subject: Re: moveto(x,y) method --20cf30334973c6a68204b801a56c Content-Type: text/plain; charset=UTF-8 On Thu, Feb 2, 2012 at 9:56 PM, luk malcik wrote: > How can I declare moveto method if I want to change co-ordinates (x,y) > that are actually in object. That's my code: > > > module Domieszka > You probably want a class here, so you can make new instances of it. I could be a module, but then you need to include it and that is more complicated at this learning level . attr_accessor :@x > attr_accessor :@y > This should be attr_accessor :x, :y > > def inspect > "You are in: #{@x} #{@y}" > end > > def moveto(x,y) > You have 2 choices. use the instance variable or use the accessors. I demonstrate below with the instance variables: @x, @y = x,y > > end > end > > > For example if I wrote i=sthClass.new > i.x = 4 > i.y = 5 > I have (4,5) object, and how to change x and y in object? > A full demo is this: $ irb ... class Domieska attr_accessor :x, :y def move_to(x,y) @x, @y = x,y end end 018:0> d = Domieska.new => # 019:0> d.move_to(10,20) => [10, 20] 021:0> d.instance_variable_get(:@x) => 10 Actually here, in the more advanced "instance_variable_get" you need the special :@x notation. HTH, Peter --20cf30334973c6a68204b801a56c--