Hello,

On Fri, May 25, 2012 at 8:38 AM, Robinson 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
>  attr_accessor: attribute
>
>  def initialize (number)
>    @attribute = number
>  end
>
>  def get_attribute
>    return @attribute
>  end
> end
>
> object = Klass.new(5)
>
> def adder(attribute)
>  return attribute += 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=(value)
    @attribute = 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,

-- 
Duong Quang Ha (H Quang Dng in Vietnamese)
blog.haqduong.com