Modify an attribute in a method?

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.

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 :slight_smile:

···

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

Hello,

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 :slight_smile:

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

Best regards,

···

On Fri, May 25, 2012 at 8:38 AM, Robinson Risquez <lists@ruby-forum.com>wrote:

--
Duong Quang Ha (Hà Quang Dương in Vietnamese)
blog.haqduong.com

Hope this helps .....

class SomeClass
  attr_accessor :attribute

  def initialize(attribute)
    @attribute = attribute
  end
end

val = SomeClass.new(5)

k=0
while (k<10) do
  puts val.attribute+=1
  k+=1
end

cheers,
saji

···

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:

----------- CLIPPED ----------

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.

--

Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan

Tel: +81242 37-2736
Fax:+81242 37-2760
email: saji@u-aizu.ac.jp
url: http://www.u-aizu.ac.jp
bib: Web of Science

k=0
while (k<10) do
  puts val.attribute+=1
  k+=1
end

Certainly can be done in many ways, but what I want to do is spend just
an attribute as a parameter to a method of the form: object.attribute,
then you can modify this attribute in the method, as follows:

def adder (attribute)
   attribute + = 1
end

That's what I do, but can not find the way.

···

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

then may be this is better ....

class SomeClass
  attr_accessor :attribute

  def initialize(attribute)
    @attribute = attribute
  end

  def adder(attribute)
    self.attribute += 1
  end
end

val = SomeClass.new(5)

k=0
while (k<10) do
  puts val.adder(:attribute)
  k+=1
end

  without the 'self', ruby interprets "attribute" to be a local variable!

saji

···

On Fri, May 25, 2012 at 12:14 PM, Robinson Risquez <lists@ruby-forum.com>wrote:

Certainly can be done in many ways, but what I want to do is spend just
an attribute as a parameter to a method of the form: object.attribute,
then you can modify this attribute in the method, as follows:

def adder (attribute)
  attribute + = 1
end

That's what I do, but can not find the way.

--

Saji N Hameed,
ARC-ENV, Center for Advanced Information Science and Technology,
University of Aizu, Tsuruga, Ikki-machi,
Aizuwakamatsu-shi, Fukushima 965-8580,
Japan

Tel: +81242 37-2736
Fax:+81242 37-2760
email: saji@u-aizu.ac.jp
url: http://www.u-aizu.ac.jp
bib: Web of Science

Hi,

···

On Fri, May 25, 2012 at 10:14 AM, Robinson Risquez <lists@ruby-forum.com>wrote:

> k=0
> while (k<10) do
> puts val.attribute+=1
> k+=1
> end

Certainly can be done in many ways, but what I want to do is spend just
an attribute as a parameter to a method of the form: object.attribute,
then you can modify this attribute in the method, as follows:

def adder (attribute)
  attribute + = 1
end

That's what I do, but can not find the way.

I'm sorry, just to make the problem more clear.

You mean that you want to modify arbitrary attribute(s) from outside
object, dont' you?

Can you give me some examples with object's state before and after you call
this method.

Sincerely,

--
Duong Quang Ha (Hà Quang Dương in Vietnamese)
blog.haqduong.com

Robinson Risquez wrote in post #1062083:

Certainly can be done in many ways, but what I want to do is spend just
an attribute as a parameter to a method of the form: object.attribute,
then you can modify this attribute in the method, as follows:

def adder (attribute)
   attribute + = 1
end

No, you cannot do it that way.

First of all, Ruby doesn't have attributes in the sense of classical
object oriented languages like Java or C++. When you write
"object.attribute", you're actually calling a getter method:
object.attributes(). It is *not* a variable, even though the missing
parantheses make it look like it (which is intended).

Secondly: Even *if* Ruby had classical object attributes, your code
wouldn't work. When you call a method and use a variable as an argument,
then the *content* of the variable will be passed to the method, not the
variable itself.

For example:

···

#---------------
def my_method(arg)
  p arg
end

my_var = 1
my_method(my_var)
#---------------

This will pass the Integer 1 to my_method. The variable itself is *not*
passed, so you cannot reassign it in the method.

What you can do is to pass an object and the name of an attribute and
then let the method call the correspoding setter method:

#---------------
class A
  attr_accessor :x
  def initialize
    @x = 0
  end
end

def addr(object, attribute_name)
  # call the getter method
  current_value = object.public_send(attribute_name)
  # call the setter method
  object.public_send("#{attribute_name}=", current_value + 1)
end

a = A.new()
puts a.x
addr(a, :x)
puts a.x
#---------------

However, I don't find this a very good programming style.

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