Forum: Ruby Change the value of an attribute-object inside a method?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
F0e2402b20933bf854ba0fb2fdcbacd6?d=identicon&s=25 Carlos Ortega (caof2005)
on 2007-04-13 21:54
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!
Ad7805c9fcc1f13efc6ed11251a6c4d2?d=identicon&s=25 Alex Young (regularfry)
on 2007-04-13 22:09
(Received via mailing list)
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.
D66d5955228e3efaa7a9122c69b3592f?d=identicon&s=25 Nando Sanchez (nando)
on 2007-04-14 00:02
###--- 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
This topic is locked and can not be replied to.