From: Josh Cheek Date: 2012-02-03T13:00:46+09:00 Subject: Re: problem with method --0016e6de00ec50062904b8075b69 Content-Type: text/plain; charset=ISO-8859-1 On Thu, Feb 2, 2012 at 8:55 PM, 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 > > -- > 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. --0016e6de00ec50062904b8075b69--