Skip to content

Instantly share code, notes, and snippets.

@RichOrElse
Last active September 3, 2018 14:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RichOrElse/452e5f9f35f16c3fb6ef6d3ab41a173c to your computer and use it in GitHub Desktop.
Save RichOrElse/452e5f9f35f16c3fb6ef6d3ab41a173c to your computer and use it in GitHub Desktop.
Refinements to use Object#b, Object#to_be and Array#or_be_one
module Be
refine Object do
def be(or_not_to_be = self, *)
or_not_to_be
end
def to_be(&to)
to.be(self)
end
end
refine Proc do
alias_method :be, :call
end
refine Method do
alias_method :be, :call
end
refine TrueClass do
def not_be(so) yield end
def either(so) so end
def either_be(so, or_be:) so.be end
end
refine FalseClass do
def not_be(so) so end
def either(so) yield end
def either_be(so, or_be:) or_be.be end
end
refine Array do
def or_be_one(&just)
one? ? just.be(first) : self
end
def or_be_none(&just)
none?.either_be -> { just.be(self) }, or_be: self
end
def or_be_any(&just)
any?.not_be(self) { just.be(self) }
end
alias_method :or_just_one, :or_be_one
alias_method :or_nothing, :or_be_none
alias_method :or_something, :or_be_any
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment