Skip to content
/ maybe2 Public

Yet another Maybe monad implementation for Ruby.

License

Notifications You must be signed in to change notification settings

ollie/maybe2

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Maybe2 Build Status Code Climate Gem Version

Wrap a value into a Maybe monad (or Optional type). Since monads are chainable and every value is wrapped in one, things don't blow up with NoMethodError on nils.

Useful for retrieving something in deeply nested structures where nils could be produced along the way.

Usage

maybe = Maybe.new('Hello world').upcase.reverse
maybe.class  # => Maybe
maybe.unwrap # => 'Hello world'

maybe = Maybe.new(nil).upcase.reverse
maybe.class  # => Maybe
maybe.unwrap # => nil

maybe = Maybe.new('Hello world').make_nil.upcase.reverse
maybe.class  # => Maybe
maybe.unwrap # => nil

JSON example evaluation to a string:

response = {
  data: {
    item: {
      name: 'Hello!'
    }
  }
}

Maybe.new(good_response)[:data][:item][:name].upcase.unwrap # => 'HELLO!'

JSON example evaluating to nil:

response = {
  data: {}
}

Maybe.new(good_response)[:data][:item][:name].upcase.unwrap # => nil

Installation

Add this line to your application's Gemfile:

gem 'maybe2'

And then execute:

$ bundle

Or install it yourself as:

$ gem install maybe2

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bin/console for an interactive prompt that will allow you to experiment.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ollie/maybe2.

License

The gem is available as open source under the terms of the MIT License.

About

Yet another Maybe monad implementation for Ruby.

Resources

License

Stars

Watchers

Forks

Packages

No packages published