I'm assuming you meant for @name
to be an instance variable, rather than a class instance one...if my hunch is right, you can do this using #send
:
class Child
def initialize
@name = "Bastion, please, call my name!"
end
attr_reader :name
private
attr_writer :name
end
class Parent
def initialize
@name = "I am a parent"
end
def rename_child(child, name)
child.send(:name=, name)
end
end
bastion, princess = Parent.new, Child.new
p princess.name
#=> "Bastion, please, call my name!"
bastion.rename_child(princess, "Moon Child")
p princess.name
#=> "Moon Child"