From: Duong Quang Ha Date: 2012-05-25T11:22:41+09:00 Subject: Re: Modify an attribute in a method? --e89a8ff1c3d0e5077d04c0d30a7f Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hello, On Fri, May 25, 2012 at 8:38 AM, Robinson Risquez wro= te: > Hi all, I write the following problem to see if you can clarify me: > How I can modify an attribute of an object in a method?, Eg: > > class Klass > attr_accessor: attribute > > def initialize (number) > @attribute =3D number > end > > def get_attribute > return @attribute > end > end > > object =3D Klass.new(5) > > def adder(attribute) > return attribute +=3D 1 > end > > while true > puts adder(objeto.get_attribute) > end > > I explain: I want the attribute @attribute (which I pass as a parameter > to the method adder) will be modified in the method adder (adding 1) > constantly, printing its output in the while cycle. > > If you use attr_accessor, it just generate something like below: # getter def attribute @attribute end # setter def attribute=3D(value) @attribute =3D value end So you can do anything you like with setter method. (If I didn't misunderstand your requirement) > In this case only 6 returns me constantly, that is, sum once and then > stop adding. As I can do in this case? > > Sorry for bad english :) > > -- > Posted via http://www.ruby-forum.com/. > > Best regards, --=20 Duong Quang Ha (H=C3=A0 Quang D=C6=B0=C6=A1ng in Vietnamese) blog.haqduong.com --e89a8ff1c3d0e5077d04c0d30a7f Content-Type: text/html; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Hello,

On Fri, May 25, 2012 at 8:38 AM, R= obinson Risquez <lists@ruby-forum.com> wrote:
Hi all, I write the following problem to see if you can clarify me:
How I can modify an attribute of an object in a method?, Eg:

class Klass
=C2=A0attr_accessor: attribute

=C2=A0def initialize (number)
=C2=A0 =C2=A0@attribute =3D number
=C2=A0end

=C2=A0def get_attribute
=C2=A0 =C2=A0return @attribute
=C2=A0end
end

object =3D Klass.new(5)

def adder(attribute)
=C2=A0return attribute +=3D 1
end

while true
=C2=A0puts adder(objeto.get_attribute)
end

I explain: I want the attribute @attribute (which I pass as a parameter
to the method adder) will be modified in the method adder (adding 1)
constantly, printing its output in the while cycle.


If you use attr_accessor, it just gene= rate something like below:

# getter
def = attribute
=C2=A0 =C2=A0 @attribute
end

# setter
def attribute=3D(value)
=C2=A0 =C2=A0 @attribu= te =3D value
end

So you can do anything = you like with setter method.
(If I didn't misunderstand your = requirement)
=C2=A0
In this case only 6 returns me constantly, that is, sum once and then
stop adding. As I can do in this case?

Sorry for bad english :)

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




= Best regards,

--
Duong Quang Ha (H=C3=A0 Quang D=C6= =B0=C6=A1ng in Vietnamese)
blog.haqduong.com

--e89a8ff1c3d0e5077d04c0d30a7f--