Reputation: 2037
I am practicing with different design patterns in Ruby and am trying to figure out the innards of Ruby. Right now I am practicing with Proxy patterns. If I have my main object:
class BankAccount
attr_reader :balance
def initialize(starting_balance = 0)
@balance = starting_balance
end
def deposit(amount)
@balance += amount
end
# other code
end
And my Proxy object:
class BankAccountProxy
def initialize(real_object)
@real_object = real_object
end
def balance
@real_object.balance
end
def deposit(amount)
@real_object.deposit(amount)
end
# other code
end
And I initialize the main and proxy object:
main = BankAccount.new(100)
proxy = BankAccountProxy.new(main)
Does Ruby make a copy of the main object within the proxy object, and therefore take up memory OR does Ruby simply point to main object from the proxy, and therefore not take up additional space?
Upvotes: 0
Views: 278
Reputation: 230306
Yes, it's called "references". Ruby will not make a copy of main
here. Or anywhere else. It's always references. If you need a copy, you do it yourself.
Upvotes: 1
Reputation: 30056
Reference to that object. "Immediate values" are just integer and symbols in Ruby.
Upvotes: 1