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

kickstarter/ruby-maybe

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

26 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

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 Gemfile
gem 'ksr-maybe'

Usage

Maybe is useful for handling potentially null values:

User = Struct.new(:email, :name)

users = [
  User.new("[email protected]", "Jane Doe"),
  User.new("[email protected]", "John Doe")
]

# When value is not null

user = users.find { |user| user.email == "[email protected]" }

Maybe.from_nullable(user)
  .map { |user| user.name }
  .get_or_else { "User not found" }

#=> "Jane Doe"

# When value is null

user = users.find { |user| user.email == "[email protected]" }

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.