ImmutableRecord is a small gem for immutable values in Ruby, inspired by Clojure’s records. ImmutableRecord.new(:foo, :bar) returns a class with readers only, a constructor that rejects missing or unknown keys, and equality by value, so records work as hash keys. Instances are frozen; to change something, clone returns a new record.

Like Clojure’s data, a record prints as code that builds it: copy what IRB shows you, paste it back in, and you get an equal value.

Item = ImmutableRecord.new(:foo, :bar)
item = Item.new(foo:1, bar:2)
#=> Item[{:foo=>1, :bar=>2}]

item.clone(foo: "changed")
#=> Item[{:foo=>"changed", :bar=>2}]

It started as a gist last autumn.

Install it with gem install immutable_record, or see it on RubyGems.