From: Shawn Anderson Date: 2012-08-31T11:03:04+09:00 Subject: Re: Attributes values influences another attribute --bcaec54ee71817a95e04c8863132 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable #1 You could also just calculate total whenever it's asked for: class Thing attr_accessor :att1, :att2, :att3 def total @att1 + @att2 + @att3 end end #2 You could use the kvo gem: https://github.com/shawn42/kvo It is generally intended for external things to watch for attribute changes, but could be used on itself. The caveat being that you don't access the instance variables directly, but must set them via their setter method. class Thing include Kvo kvo_attr_accessor :att1, :att2, :att3 def initialize self.when :attr1_changed { update_total } self.when :attr2_changed { update_total } self.when :attr3_changed { update_total } end def update_total @total =3D att1 + att2 + att3 end end If you are only calculating a quick sum, option #1 should suffice. If you are actually doing some complicated thing that needs to only be calculated once, #2 would work. Option #3 (not shown) would be to just write the setter methods your self for att1=3D, att2=3D ... etc Good luck /Shawn On Thu, Aug 30, 2012 at 9:38 PM, Dami=E1n M. Gonz=E1lez wrote: > Ey guys, how are you? I'm wondering if you can give me your advice. > First the problem: > > . > . > attr_accessor :attribute1 :attribute2 :attribute3 > . > . > @attribute1 =3D 3 > @attribute2 =3D 5 > @attribute3 =3D 1 > @total =3D 9 > #this last attribute must be the result of the summation of @attribute1 > + #@attribute2 + @attribute3. > . > . > > So what's the best way to reflect the explained in the comments inside > the code. Will be very nice if i can assign the value of attribute1, 2 > or 3 with the attr_accessor facility. How can the value of @total be > refreshed when one of attribute1, 2 or 3 has his value changed? I though > in do(instead of the attr_accessor facility) one writer method for each > of the 3 attribute, like this: > > def change_attribute1(new_value) > @total =3D @attribute2 + @attribute3 + new_value > @attribute1 =3D new_value > end > #and so on with the others attributes > > but this seems sicken. Is there any way to link the values of the > attributes with @total with the attr_accessor facility or other way...or > I'm just limited to the last option. Thank's for your time. Dami=E1n. > > -- > Posted via http://www.ruby-forum.com/. > --bcaec54ee71817a95e04c8863132 Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable
#1
You could also just calculate total whenever it's asked for:<= br>
class Thing
=A0 attr_accessor :att1, :att2, :att3
=A0 def tota= l
=A0=A0=A0 @att1 + @att2 + @att3
=A0 end
end

#2
You cou= ld use the kvo gem: https://gith= ub.com/shawn42/kvo=A0 It is generally intended for external things to w= atch for attribute changes, but could be used on itself.

The caveat being that you don't access the instance variables direc= tly, but must set them via their setter method.

class Thing
=A0 i= nclude Kvo
=A0 kvo_attr_accessor :att1, :att2, :att3
=A0 def initiali= ze
=A0=A0=A0 self.when :attr1_changed { update_total=A0 }
=A0=A0=A0 self.wh= en :attr2_changed { update_total=A0 }
=A0=A0=A0 self.when :attr3_changed= { update_total=A0 }
=A0 end

=A0 def update_total
=A0=A0=A0 @t= otal =3D att1 + att2 + att3
=A0 end
end

If you are only calculating a quick sum, option #1 should suffic= e. If you are actually doing some complicated thing that needs to only be c= alculated once, #2 would work. Option #3 (not shown) would be to just write= the setter methods your self for att1=3D, att2=3D ... etc

Good luck
/Shawn

On Thu, Aug 30, 2= 012 at 9:38 PM, Dami=E1n M. Gonz=E1lez <lists@ruby-forum.com> wrote:
Ey guys, how are you? I'm wondering if y= ou can give me your advice.
First the problem:

.
.
attr_accessor :attribute1 :attribute2 :attribute3
.
.
@attribute1 =3D 3
@attribute2 =3D 5
@attribute3 =3D 1
@total =3D 9
#this last attribute must be the result of the summation of @attribute1
+ #@attribute2 + @attribute3.
.
.

=A0So what's the best way to reflect the explained in the comments insi= de
the code. Will be very nice if i can assign the value of attribute1, 2
or 3 with the attr_accessor facility. How can the value of @total be
refreshed when one of attribute1, 2 or 3 has his value changed? I though in do(instead of the attr_accessor facility) one writer method for each
of the 3 attribute, like this:

def change_attribute1(new_value)
=A0@total =3D @attribute2 + @attribute3 + new_value
=A0@attribute1 =3D new_value
end
#and so on with the others attributes

=A0but this seems sicken. Is there any way to link the values of the
attributes with @total with the attr_accessor facility or other way...or I'm just limited to the last option. Thank's for your time. Dami=E1= n.

--
Posted via http://= www.ruby-forum.com/.

--bcaec54ee71817a95e04c8863132--