# Change the value of an attribute-object inside a method?

**URL:** <https://www.ruby-forum.com/t/change-the-value-of-an-attribute-object-inside-a-method/91971>\
**Category:** Ruby\
**Created:** [April 13, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/change-the-value-of-an-attribute-object-inside-a-method/91971 "2007-04-13T21:54:36Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [April 13, 2007, 9:54pm UTC](https://www.ruby-forum.com/t/change-the-value-of-an-attribute-object-inside-a-method/91971/1 "2007-04-13T21:54:36Z")

</div>

Folks:

This is a simple newbe question on how to change the value of an  
attribute and I have tried to do it in several ways without success.  
Here is the code:

###— Main Class —###  
class Employee  
attr\_writer :name  
attr\_reader :name

def name  
@name  
end

def initialize(name)  
puts “Creating object…”  
@name=name  
puts “Object created!”  
end  
end

def method\_to\_call( parameter1 )  
objectEmployee = Employee.new( parameter1 )  
puts " Name:=" + objectEmployee.name  
puts  
yield( objectEmployee.name )  
puts  
puts " Name:=" + objectEmployee.name  
puts  
end

# method\_to\_call(“Carlos”) do |paramA| paramA = paramA + " Aquiles" puts " Trying to modify parameter!..!" puts " =\> paramA := " + paramA end

The output is:

Creating object…  
Object created!  
Name:=Carlos

```
                   Trying to modify parameter!....!
                   => paramA := Carlos Aquiles

```

# Name:=Carlos

So the question is how can I pass an attribute or Object to a method and  
that inside it I can change the value of the attribute or Object, so  
after leaving the method the attribute or object gets updated.

Any help will be very appreciated!

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [April 13, 2007, 10:09pm UTC](https://www.ruby-forum.com/t/change-the-value-of-an-attribute-object-inside-a-method/91971/2 "2007-04-13T22:09:19Z")

</div>

Carlos Caof2005 wrote:

> method\_to\_call(“Carlos”) do |paramA|
> 
> ```
> Trying to modify parameter!....!
> => paramA := Carlos Aquiles
> 
> ```
> 
> # Name:=Carlos
> 
> So the question is how can I pass an attribute or Object to a method and  
> that inside it I can change the value of the attribute or Object, so  
> after leaving the method the attribute or object gets updated.  
> You need to actually call a method on the object which changes it. What  
> you’re doing when you say:

paramA = paramA + " Aquiles"

is actually this:

paramA = paramA.+(" Aquiles")

The convention is that the ‘+’ method returns a new object that is the  
result of the ‘+’ operation, so this new object then replaces the  
previous paramA when you do the assignment. What you actually want in  
this case is:

paramA \<\< " Aquiles"

The ‘\<\<’ method actually alters the contents of the string rather than  
allocating a new one.

---

<div class="post-metadata">

**Author:** ![caof2005](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/97f17d/32.png) [@caof2005](https://www.ruby-forum.com/u/caof2005)\
**Post date:** [April 14, 2007, 12:02am UTC](https://www.ruby-forum.com/t/change-the-value-of-an-attribute-object-inside-a-method/91971/3 "2007-04-14T00:02:14Z")

</div>

###— Main Class —###  
class Employee  
attr\_accessor :name

def initialize(name)  
puts “Creating object…”  
@name=name  
puts “Object created!”  
end  
end

def method\_to\_call( parameter1 )  
objectEmployee = Employee.new( parameter1 )  
puts " Name:=" + objectEmployee.name  
puts  
yield( objectEmployee.name )  
puts  
puts " Name:=" + objectEmployee.name  
puts  
end

method\_to\_call(“Carlos”) do |paramA|  
paramA \<\< " Aquiles"  
puts " Trying to modify parameter!..!"  
puts " =\> paramA := " + paramA  
end
