Skip to content

Instantly share code, notes, and snippets.

@RichOrElse RichOrElse/be.rb
Last active Sep 3, 2018

Embed
What would you like to do?
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
You can’t perform that action at this time.