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
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.