On Fri, Feb 03, 2012 at 11:55:13AM +0900, luk malcik wrote:
@x = x
end
end
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”.
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
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.
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.