A nicer replacement for Ruby's Struct and OpenStruct.
MIT License
A nicer replacement for Ruby's Struct and OpenStruct.
Isn't it annoying how OpenStruct.new
takes a hash, but the arguments to Struct.new
have to be ordered?
Do you find you always use an OpenStruct
when you don't really care if it's open, you just want an easy way to create an object from a hash?
Did you know that creating an OpenStruct
blows away Ruby's method cache?
Isn't it irritating how you have to remember that, to use OpenStruct
, you need to require 'ostruct'
?
Have you ever wished you could create a Struct
or OpenStruct
, but make it immutable?
Add this line to your application's Gemfile:
gem 'finer_struct'
And then execute:
$ bundle
Or install it yourself as:
$ gem install finer_struct
Immutable structs can't have their attributes changed after creation.
struct = FinerStruct::Immutable.new(a: 1, b: 2)
struct.a # => 1
struct.b # => 2
struct.a = 3 # Exception!
class MyStruct < FinerStruct::Immutable(:a, :b); end
struct = MyStruct.new(a: 1, b: 2)
MyStruct.new(a: 1, b: 2, c: 3) # Exception!
Mutable structs let you assign attributes at any time.
struct = FinerStruct::Mutable.new(a: 1, b: 2)
struct.a = 3
struct.a # => 3
class MyStruct < FinerStruct::Mutable(:a, :b); end
struct = MyStruct.new(a: 1, b: 2)
git checkout -b my-new-feature
)git commit -am 'Add some feature'
)git push origin my-new-feature
)