Moveto(x,y) method

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
attr_accessor :@x
attr_accessor :@y

def inspect
“You are in: #{@x} #{@y}”
end

def moveto(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?

On Fri, Feb 03, 2012 at 05:56:29AM +0900, luk malcik wrote:

end

def moveto(x,y)

end
end

module Domieszka
attr_accessor :x
attr_accessor :y

assuming @x and @y are defined somewhere, perhaps in a class

where you plan to include this module

def inspect
“You are in #{@x} #{@y}”
end

def move_to(x,y)
@x = x
@y = y
end
end

On Thu, Feb 2, 2012 at 9:56 PM, luk malcik [email protected] 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
=> #Domieska:0x8c2c150
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

ok, than you very much. It must be a module because it will be used in
classes like triangle square or circle. Include will be used in each
one.
Could You tell me one more? Is this accessors could be use in Cirle
class to define ray? for example co-ordinates i(4,5) z(6 9) do the ray
with length x

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs