Project

ksr-maybe

0.0
Repository is archived
No commit activity in last 3 years
No release in over 3 years
A library providing the optional type 'Maybe'
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 5.10
~> 12.0

Runtime

~> 0.16.0
 Project Readme

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("jane@doe.com", "Jane Doe"),
  User.new("john@doe.com", "John Doe")
]

# When value is not null

user = 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 null

user = 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.