DEV Community

Discussion on: Flexible Ruby Value Object Initialisation

Collapse
 
philnash profile image
Phil Nash

Nice use of refinements. I've not seen them around much, but this is a good example of how they can really clean up an approach.

Collapse
 
databasesponge profile image
MetaDave 🇪🇺 • Edited

Thanks Phil. I like the way they put the Ruby core objects on a level playing field with your own application objects, and let them add the behaviour you want.

Having said that, I'd probably draw a line at doing anything like:

module EasyFinder
  refine Integer do
    def to_book
      Book.find(self)
    end
  end

  refine String do
    def to_book
      Book.find(self.to_i)
    end
  end
end

... if I thought anyone was ever going to see my code.

I like the semantics, but it's a bit "out there".