Why method pole= doesn't work??
Kolo = Struct.new (:x,:y,:r)
Kwadrat = Struct.new (:x,:y,:a)
Prostokat = Struct.new (:x,:y,:a,:b)
module Domieszka
attr_accessor :x, :y
def moveto(x,y)
@x = x
@y = y
end
end
class Kolo
include Domieszka
def pole=
@r = r
pole = r * Math.PI
return pole
end
end
···
--
Posted via http://www.ruby-forum.com/.
I'm not entirely sure what you're trying to do. Does this accomplish
what you want?
class Kolo
include Domieszka
def pole=(r)
@r = r
r * Math::PI
end
end
Note that PI is a constant within Math's namespace, and not a method.
The period is used as a method call operator; the double-colon is a
namespace separator, used to access constants within the module's
namespace.
Also note that the pole= definition needs to have an argument defined for
it if you want to use that argument -- which I think is what you were
trying to do with that "r".
···
On Fri, Feb 03, 2012 at 11:55:13AM +0900, luk malcik wrote:
Why method pole= doesn't work??
Kolo = Struct.new (:x,:y,:r)
Kwadrat = Struct.new (:x,:y,:a)
Prostokat = Struct.new (:x,:y,:a,:b)
module Domieszka
attr_accessor :x, :y
def moveto(x,y)
@x = x
@y = y
end
end
class Kolo
include Domieszka
def pole=
@r = r
pole = r * Math.PI
return pole
end
end
--
Chad Perrin [ original content licensed OWL: http://owl.apotheon.org ]
I mean that Pole= is the same as Pole_is. I want to declare accessors to
write pole and read pole: pole= and pole. Of course Math::PI * r**
// my mistake
···
--
Posted via http://www.ruby-forum.com/.
pole= is the name of the method
···
--
Posted via http://www.ruby-forum.com/.
There's a bunch of things here to pay attention to.
* Methods ending in equal signs need paramaters. e.g. `def pole=(radius)`
* Assignments in Ruby always return the RHS of the assignment, so your
explicit `return pole` won't work.
* If you want the parentheses to delimit a method call, you must put them
directly after the name (to avoid ambiguous syntax). So this means
`Struct.new(:a, :b, :c)` not `Struct.new (:a, :b, :c)`
* Structs don't store their data in instance variables of the given name,
so when you say `@r = r` you're not setting the variable that will be
returned when the method r is invoked. Instead, use the setter that the
struct provides (I consider this the right way to set things, anyway) So
`self.r = r`
* There are some other options for your Kolo class that I think wold be
better:
Kolo = Struct.new :x, :y, :r do
def pole=(...)
...
end
end
or alternatively
class Kolo < Struct.new(:x, :y, :z)
def pole=(...)
...
end
end
I like both of these better than reopening the class later.
···
On Thu, Feb 2, 2012 at 8:55 PM, luk malcik <aport99@gmail.com> wrote:
Why method pole= doesn't work??
Kolo = Struct.new (:x,:y,:r)
Kwadrat = Struct.new (:x,:y,:a)
Prostokat = Struct.new (:x,:y,:a,:b)
module Domieszka
attr_accessor :x, :y
def moveto(x,y)
@x = x
@y = y
end
end
class Kolo
include Domieszka
def pole=
@r = r
pole = r * Math.PI
return pole
end
end
--
Posted via http://www.ruby-forum.com/\.