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

**URL:** <https://rubytalk.org/t/change-the-value-of-an-attribute-object-inside-a-method/36924>\
**Category:** ruby-talk\
**Created:** [13 April 2007 19:54 UTC](https://rubytalk.org/t/change-the-value-of-an-attribute-object-inside-a-method/36924 "2007-04-13T19:54:38Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carlos\_Caof2005](https://avatars.discourse-cdn.com/v4/letter/c/aca169/32.png) [@Carlos\_Caof2005](https://rubytalk.org/u/Carlos_Caof2005)\
**Post date:** [13 April 2007 19:54 UTC](https://rubytalk.org/t/change-the-value-of-an-attribute-object-inside-a-method/36924/1 "2007-04-13T19:54:38Z")

</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  
&nbsp;&nbsp;attr\_writer :name  
&nbsp;&nbsp;attr\_reader :name

&nbsp;&nbsp;def name  
&nbsp;&nbsp;&nbsp;&nbsp;@name  
&nbsp;&nbsp;end

&nbsp;&nbsp;def initialize(name)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Creating object...."  
&nbsp;&nbsp;&nbsp;&nbsp;@name=name  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Object created!"  
&nbsp;&nbsp;end  
end

def method\_to\_call( parameter1 )  
&nbsp;&nbsp;&nbsp;objectEmployee = Employee.new( parameter1 )  
&nbsp;&nbsp;&nbsp;puts " Name:=" + [objectEmployee.name](http://objectEmployee.name)  
&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;&nbsp;yield( [objectEmployee.name](http://objectEmployee.name) )  
&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;&nbsp;puts " Name:=" + [objectEmployee.name](http://objectEmployee.name)  
&nbsp;&nbsp;&nbsp;puts  
end

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

> **···**
>
> ==================  
> The output is:
> 
> Creating object....  
> Object created!  
> Name:=Carlos
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Trying to modify parameter!....!  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=\> 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!
> 
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

---

<div class="post-metadata">

**Author:** ![Alex\_Young](https://avatars.discourse-cdn.com/v4/letter/a/ee7513/32.png) [@Alex\_Young](https://rubytalk.org/u/Alex_Young)\
**Post date:** [13 April 2007 20:08 UTC](https://rubytalk.org/t/change-the-value-of-an-attribute-object-inside-a-method/36924/2 "2007-04-13T20:08:18Z")

</div>

Carlos Caof2005 wrote:

> 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  
> &nbsp;&nbsp;attr\_writer :name  
> &nbsp;&nbsp;attr\_reader :name
> 
> &nbsp;&nbsp;def name  
> &nbsp;&nbsp;&nbsp;&nbsp;@name  
> &nbsp;&nbsp;end
> 
> &nbsp;&nbsp;def initialize(name)  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Creating object...."  
> &nbsp;&nbsp;&nbsp;&nbsp;@name=name  
> &nbsp;&nbsp;&nbsp;&nbsp;puts "Object created!"  
> &nbsp;&nbsp;end  
> end
> 
> def method\_to\_call( parameter1 )  
> &nbsp;&nbsp;&nbsp;objectEmployee = Employee.new( parameter1 )  
> &nbsp;&nbsp;&nbsp;puts " Name:=" + objectEmployee.name  
> &nbsp;&nbsp;&nbsp;puts  
> &nbsp;&nbsp;&nbsp;yield( objectEmployee.name )  
> &nbsp;&nbsp;&nbsp;puts  
> &nbsp;&nbsp;&nbsp;puts " Name:=" + objectEmployee.name  
> &nbsp;&nbsp;&nbsp;puts  
> end
> 
> # method\_to\_call("Carlos") do |paramA| &nbsp;&nbsp;&nbsp;&nbsp;paramA = paramA + " Aquiles" &nbsp;&nbsp;&nbsp;&nbsp;puts " Trying to modify parameter!....!" &nbsp;&nbsp;&nbsp;&nbsp;puts " =\> paramA := " + paramA end
> 
> The output is:
> 
> Creating object....  
> Object created!  
> Name:=Carlos
> 
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Trying to modify parameter!....!  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;=\> 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:

&nbsp;&nbsp;&nbsp;paramA = paramA + " Aquiles"

is actually this:

&nbsp;&nbsp;&nbsp;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:

&nbsp;&nbsp;&nbsp;paramA \<\< " Aquiles"

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

> **···**
>
> --  
> Alex

---

<div class="post-metadata">

**Author:** ![Nando\_Sanchez](https://avatars.discourse-cdn.com/v4/letter/n/85e7bf/32.png) [@Nando\_Sanchez](https://rubytalk.org/u/Nando_Sanchez)\
**Post date:** [13 April 2007 22:02 UTC](https://rubytalk.org/t/change-the-value-of-an-attribute-object-inside-a-method/36924/3 "2007-04-13T22:02:16Z")

</div>

###--- Main Class ---###  
class Employee  
&nbsp;&nbsp;attr\_accessor :name

&nbsp;&nbsp;def initialize(name)  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Creating object...."  
&nbsp;&nbsp;&nbsp;&nbsp;@name=name  
&nbsp;&nbsp;&nbsp;&nbsp;puts "Object created!"  
&nbsp;&nbsp;end  
end

def method\_to\_call( parameter1 )  
&nbsp;&nbsp;&nbsp;objectEmployee = Employee.new( parameter1 )  
&nbsp;&nbsp;&nbsp;puts " Name:=" + [objectEmployee.name](http://objectEmployee.name)  
&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;&nbsp;yield( [objectEmployee.name](http://objectEmployee.name) )  
&nbsp;&nbsp;&nbsp;puts  
&nbsp;&nbsp;&nbsp;puts " Name:=" + [objectEmployee.name](http://objectEmployee.name)  
&nbsp;&nbsp;&nbsp;puts  
end

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

> **···**
>
> --  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
