<commit_before>= ImmutableStruct

{<img src="https://travis-ci.org/stitchfix/immutable-struct.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/stitchfix/immutable-struct]

Creates struct-like classes (that can build value objects) that do not have setters and also have better constructors than Ruby's built-in +Struct+.

This is highly useful for creating presenters, non-database-related models, or other quick and dirty classes in your application.  Instead of using a +Hash+ or +OpenStruct+, you can create a bit more clarity around your types by using +ImmutableStruct+, which is almost as convienient.

== Install

Add to your +Gemfile+:

    gem 'immutable-struct'

Then install:

    bundle install

If not using bundler, just use RubyGems:

    gem install immutable-struct


== To use

    Person = ImmutableStruct.new(:name, :age, :job, :active?, [:addresses]) do
      def minor?
        age < 18
      end
    end

    p = Person.new(name: "Dave",   # name will be 'Dave'
                   age: 40,        # age will be 40
                                   # job is omitted, so will be nil
                   active: true)   # active and active? will be true
                                   # addresses is omitted, but since we've selected
                                   # Array coercion, it'll be []
    p.name      # => "Dave"
    p.age       # => 40
    p.active?   # => true
    p.minor?    # => false
    p.addresses # => []

You can also treat the interior as a normal class definition.  

== Links

* rdoc[http://stitchfix.github.io/immutable-struct]
* source[http://github.com/stitchfix/immutable-struct]
* blog[http://technology.stitchfix.com/blog/2013/12/20/presenters-delegation-vs-structs/]<commit_msg>Updating README.rdoc with usage notes for new equality methods.<commit_after>= ImmutableStruct

{<img src="https://travis-ci.org/stitchfix/immutable-struct.svg?branch=master" alt="Build Status" />}[https://travis-ci.org/stitchfix/immutable-struct]

Creates struct-like classes (that can build value objects) that do not have setters and also have better constructors than Ruby's built-in +Struct+.

This is highly useful for creating presenters, non-database-related models, or other quick and dirty classes in your application.  Instead of using a +Hash+ or +OpenStruct+, you can create a bit more clarity around your types by using +ImmutableStruct+, which is almost as convienient.

== Install

Add to your +Gemfile+:

    gem 'immutable-struct'

Then install:

    bundle install

If not using bundler, just use RubyGems:

    gem install immutable-struct


== To use

    Person = ImmutableStruct.new(:name, :age, :job, :active?, [:addresses]) do
      def minor?
        age < 18
      end
    end

    p = Person.new(name: "Dave",   # name will be 'Dave'
                   age: 40,        # age will be 40
                                   # job is omitted, so will be nil
                   active: true)   # active and active? will be true
                                   # addresses is omitted, but since we've selected
                                   # Array coercion, it'll be []
    p.name      # => "Dave"
    p.age       # => 40
    p.active?   # => true
    p.minor?    # => false
    p.addresses # => []

    p2 = Person.new(name: "Dave", age: 40, active: true)

    p == p2     # => true
    p.eql?(p2)  # => true

    SimilarPerson = ImmutableStruct.new(:name, :age, :job, :active?, [:addresses])

    sp = SimilarPerson.new(name: "Dave", age: 40, active: true)

    p == sp     # => false         # Different class leads to inequality



You can also treat the interior as a normal class definition.  

== Links

* rdoc[http://stitchfix.github.io/immutable-struct]
* source[http://github.com/stitchfix/immutable-struct]
* blog[http://technology.stitchfix.com/blog/2013/12/20/presenters-delegation-vs-structs/]
{'fraction_non_alphanumeric': 0.10905349794238683, 'fraction_numerical': 0.010030864197530864, 'mean_word_length': 2.4538188277087034, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 18, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3973558', 'n_tokens_mistral': 1213, 'n_tokens_neox': 1136, 'n_words': 382}