Sha256: 342dfaba0041eaa05bad42f5f6e0d09f18aa50afbd895d0fcb29ae2f32c353c3
Contents?: true
Size: 1.62 KB
Versions: 1
Compression:
Stored size: 1.62 KB
Contents
class Object # Returns a Proc which always return the given object # # constantly_true = true.constantly # constantly_true.call(1234) => true # constantly_true.call(nil) => true # constantly_true.call('HOLA') => true def constantly Proc.new { self } end # Returns a Proc which negates the result of calling the argumented # callable object. # # :positive?.complement.call(1) => false # :nil?.complement.call(nil) => false def complement callable = case when respond_to?(:call) self else self.to_proc end ->(*args, **kwargs, &blk) { !callable.call(*args, **kwargs, &blk) } rescue NoMethodError => error case error.name when :to_proc raise ArgumentError, "Object is not 'Callable' so it can't have a complement function", caller else raise end end # Applies the given block to the object if the condition applied on # it holds true. If not, it defaults to the object as if `then_if` # was not called. # # +1.then_if(:positive?) { _1 * 1000 } => 1_000 # -1.then_if(:positive?) { _1 * 1000 } => -1 def then_if(condition) if condition.to_proc.call(self) yield self else self end end # Applies the given block to the object if the condition applied on # it doesn't hold true. If not, it defaults to the object as if # `then_unless` was not called. # # +1.then_if(:positive?) { _1 * 1000 } => 1_000 # -1.then_if(:positive?) { _1 * 1000 } => -1 def then_unless(condition, &block) then_if(condition.complement, &block) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
jgomo3-func-0.1.0 | lib/jgomo3/func/core_ext/object.rb |