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

Discussion in 'Ruby' started by Carlos Caof2005, Apr 13, 2007.

  1. 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!

    --
    Posted via http://www.ruby-forum.com/.
    Carlos Caof2005, Apr 13, 2007
    #1
    1. Advertising

  2. Carlos Caof2005

    Alex Young Guest

    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
    > 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.

    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.

    --
    Alex
    Alex Young, Apr 13, 2007
    #2
    1. Advertising

  3. ###--- 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

    --
    Posted via http://www.ruby-forum.com/.
    Nando Sanchez, Apr 13, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    1
    Views:
    4,264
    Joe Kesselman
    Aug 2, 2006
  2. Replies:
    8
    Views:
    521
  3. Marc Aymerich
    Replies:
    0
    Views:
    70
    Marc Aymerich
    Nov 24, 2013
  4. Devin Jeanpierre
    Replies:
    12
    Views:
    105
    Steven D'Aprano
    Nov 24, 2013
  5. Chris Angelico
    Replies:
    0
    Views:
    58
    Chris Angelico
    Nov 24, 2013
Loading...

Share This Page