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
change the attribute of a value object, you should create a new value
Otherwise you will break the rule of value object . But we know in
setter method always return the value it sets.
attr_reader :amount, :currency
def initialize(amount, currency)
@amount = amount
@currency = currency
self.class == other_money.class && amount == other_money.amount &&
currency == other_money.currency
alias :eql? :==
usd1 = Money.new(10, ‘usd’)
usd2 = Money.new(10, ‘usd’)
usd1.eql?(usd2) # => true
usd1 == usd2 # => true
usd = Money.new(10, ‘USD’)
other_usd = (usd.amount = 20)
Look the output of other_usd.inspect which returns “20”, as per the
should return a new Money object.
My question is how then we implement/correct this flaw, which author
Debugging is twice as hard as writing the code in the first place.
if you write the code as cleverly as possible, you are, by definition,
smart enough to debug it.