C
Carlos Caof2005
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!
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!