On Mon, Sep 1, 2014 at 4:43 AM, Arup Rakshit <aruprakshit / rocketmail.com> wrote:

> I am reading currently one design pattern called the _value object_. To
> understand it I am reading a blog post http://www.sitepoint.com/value-objects-explained-with-ruby/, 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.

> My question is how then we implement/correct this flaw, which author missed to
> mention ?

Do not use an assignment method then. :-)  As Bryce mentioned one
approach is to provide proper mathematical constructs.  I wrote about
this once:
http://blog.rubybestpractices.com/posts/rklemme/019-Complete_Numeric_Class.html

For changing the currency you probably need an explicit conversion method, e.g.

def to_currency(target_currency, conversion_factor = 1)
  self.class.new[amount * conversion_factor, target_currency]
end

Btw. to make the approach more robust you can freeze the instance at
the end of #initialize.

Kind regards

robert


-- 
[guy, jim].each {|him| remember.him do |as, often| as.you_can - without end}
http://blog.rubybestpractices.com/