Be cautious with Ruby coercion methods

Coercion is a tricky problem to solve and it鈥檚 something we need pretty much everywhere. Whether you鈥檙e building a web application or a CLI tool, you will have to coerce values in some way. I say it鈥檚 a tricky problem because Ruby comes with many builtin coercion methods but it鈥檚 not enough. Furthermore, the builtin methods may actually lead to bugs or surprising behaviors. In this article we鈥檒l take a look at the builtin coercion methods, various caveats that come with their usage and ways how you can handle coercion in a more predictable and strict way....

July 29, 2020 路 5 min 路 Peter Solnica

Introducing dry-schema

Two years ago (!!!) I published a post on discourse.dry-rb.org explaining my plans for dry-validation 1.0.0 and in this post I mentioned that there will be a new gem called dry-schema that dry-validation will use for its schemas. It鈥檚 crazy how time flies because I swear I thought it was last year. I know that over this time dry-validation has been accumulating a lot of issues (125 in the moment of writing this article), and I know it sucks, big time, that I didn鈥檛 have a chance to address any of these....

January 31, 2019 路 6 min 路 Peter Solnica

Duck typing vs type safety in Ruby

Duck typing is one of the virtues of the Ruby language, it adds a lot of flexibility to the code, and allows us to use objects of different type in places where only specific methods are needed. Even though the idea behind duck typing may seem to be straight-forward, it is easy to use it incorrectly. It鈥檚 interesting to notice that over many years we鈥檝e adopted various techniques that seem to leverage duck typing, but the specifics of how exactly we鈥檙e doing it are actually questionable, and I believe they deserve some reconsideration....

November 2, 2016 路 6 min 路 Peter Solnica

Invalid Object Is An Anti-Pattern

The idea of an object that validates its own state has been made very popular by Rails鈥 ActiveRecord. We can see this pattern in many places, not only in ORM libraries but in many other gems whenever some sort of validation is needed. Have you ever thought about why we鈥檙e allowing invalid state just to鈥alidate data? It doesn鈥檛 seem to be a good idea, in fact, it feels like a huge anti-pattern....

December 28, 2015 路 4 min 路 Peter Solnica

Introducing dry-validation

We started experimenting with a new validation library under dry-rb organization a couple of months ago, and last month I released the first version of dry-validation. Since then I worked hard on improving it and adding support for i18n. Today, I released dry-validation 0.3.0 with a couple of bug fixes, new features and a complete support for i18n. Before I tell you what it is and how it can help you, let me start by explaining the reasoning behind this project....

December 7, 2015 路 8 min 路 Peter Solnica