Skip to content
This repository was archived by the owner on Feb 8, 2019. It is now read-only.

Repository files navigation

ksr-maybe

This gem provides a Maybe type. The Maybe type either contains a value (represented as Just) or it is empty (represented as Nothing).

Installation

Install the ksr-maybe gem, or add it to your Gemfile with bundler:

# In your Gemfilegem'ksr-maybe'

Usage

Maybe is useful for handling potentially null values:

User=Struct.new(:email,:name)users=[User.new("jane@doe.com","Jane Doe"),User.new("john@doe.com","John Doe")]# When value is not nulluser=users.find{ |user| user.email == "jane@doe.com"}Maybe.from_nullable(user).map{ |user| user.name}.get_or_else{"User not found"}#=> "Jane Doe"# When value is nulluser=users.find{ |user| user.email == "not@present.com"}Maybe.from_nullable(user).map{ |user| user.name}.get_or_else{"User not found"}#=> "User not found"

Contracts

This gem utilizes the 'contracts.ruby' gem, which adds dynamic type-checking to the 'ruby-maybe' codebase. By default, the 'contracts.ruby' gem will raise an error during runtime upon a type mismatch. To override this error throwing behavior, see this 'contracts.ruby' documentation.

Licenses

See LICENSE and NOTICE.

About

A Ruby implementation of the Maybe type

Topics

Resources

Stars

10 stars

Watchers

13 watching

Forks

Releases

Packages

Used by

Contributors

Languages