Hi,
I am reading currently one design pattern called the value object. To
understand it I am reading a blog post
Value Objects Explained with Ruby — SitePoint, as it
contains a Ruby example.
The problem is the author of the post said, that, whenever you will try
to
change the attribute of a value object, you should create a new value
object.
Otherwise you will break the rule of value object . But we know in
Ruby, the
setter method always return the value it sets.
#!/usr/bin/env ruby
class Money
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
end
def amount=(other_amount)
Money.new(other_amount, currency)
end
def ==(other_money)
self.class == other_money.class && amount == other_money.amount &&
currency == other_money.currency
end
alias :eql? :==
def hash
[amount, currency].hash
end
end
usd1 = Money.new(10, ‘usd’)
usd2 = Money.new(10, ‘usd’)
usd1.eql?(usd2) # => true
usd1 == usd2 # => true
usd = Money.new(10, ‘USD’)
p usd.inspect
other_usd = (usd.amount = 20)
p usd.inspect
p other_usd.inspect
>> “#<Money:0xa12f354 @amount=10, @currency="USD">”
>> “#<Money:0xa12f354 @amount=10, @currency="USD">”
>> “20”
Look the output of other_usd.inspect which returns “20”, as per the
author it
should return a new Money object.
My question is how then we implement/correct this flaw, which author
missed to
mention ?
–
Regards,
Arup R.
Debugging is twice as hard as writing the code in the first place.
Therefore,
if you write the code as cleverly as possible, you are, by definition,
not
smart enough to debug it.
–Brian Kernighan