persistent-π: a new ruby gem for beautiful immutable data structures
After a few weeks of work, I’ve just released version 1.0 of my new gem: persistent-π!
The objective of this gem is to make programming with immutable data structures in Ruby as joyful and frictionless as using the built-in Array
and Hash
classes. So easy, in fact, that using immutable structures becomes the norm, rather than the exception.
Ruby has no immutable data structures in the standard library, but it is flexible enough that we can add such functionality as a library, and disguise it so it almost looks like the built-in data structures.
For instance, with persistent-π you can create:
-
Arrays:
my_array = aπ[1, 2, 3]
-
Hashes:
my_hash = hπ[key1: 'foo', key2: 'bar']
-
Sets:
my_set = sπ[:sephiroth, :kills, :aeris]
Using an emoji as part of the gem’s api was an interesting challenge, but with some care it’s possible to support Ruby 1.9.3 to 2.5.0, and JRuby 1.7 and 9.x, although the latter have some known bugs with unicode handling.
Furthermore, this gem integrates with the concurrent-ruby and hamster gems, so you can easily convert back and forth between instances of these libraries, so you can use the best tool for the job.
This was a very fun project, and the git history reveals that it took a bit more than I expected to get it just right.
So… go forth and use it. Feedback is very welcome!