class Object
Constants
- Log
- MESSAGE_FMT
Public Class Methods
# File lib/gorillib/exception/raisers.rb, line 148 def self.abstract_method!(obj) file, line, meth = caller_parts raise AbstractMethodError.new("#{MESSAGE} -- must be implemented by the subclass" % [meth, obj, obj.class]) end
Raise an error if there are fewer arguments than expected. The message will have the same format used by Ruby internal methods. @see check_arity!
@example want to use splat args, requiring at least one
def assemble_path(*pathsegs) ArgumentError.arity_at_least!(pathsegs, 1) # ... end
@param [Array] args splat args as handed to the caller @param [Integer] val minimum expected length
# File lib/gorillib/exception/raisers.rb, line 75 def self.arity_at_least!(args, min_arity) check_arity!(args, min_arity .. Float::INFINITY) end
@return [Array] file, line, method_name
# File lib/gorillib/exception/raisers.rb, line 3 def self.caller_parts(depth=1) caller_line = caller(depth).first mg = %r{\A([^:]+):(\d+):in \`([^\']+)\'\z}.match(caller_line) or return [caller_line, 1, '(unknown)'] [mg[1], mg[2].to_i, mg[3]] rescue warn "problem in #{self}.caller_parts" return [__FILE__, __LINE__, '(unknown)'] end
Raise an error if there are a different number of arguments than expected. The message will have the same format used by Ruby internal methods. @see arity_at_least!
@example want ‘getset(:foo)` to be different from `getset(:foo, nil)`
def getset(key, *args) ArgumentError.check_arity!(args, 0..1) return self[key] if args.empty? self[key] = args.first end
@overload check_arity!(args, n)
@param [Array] args splat args as handed to the caller @param [Integer] val expected length
@overload check_arity!(args, x..y)
@param [Array] args splat args as handed to the caller @param [#include?] val expected range/list/set of lengths
@raise ArgumentError
when there are
# File lib/gorillib/exception/raisers.rb, line 56 def self.check_arity!(args, val, &block) allowed_arity = val.is_a?(Integer) ? (val..val) : val return true if allowed_arity.include?(args.length) info = " #{block.call}" rescue nil if block_given? raise self.new("wrong number of arguments (#{args.length} for #{val})#{info}") end
Raise an error with the same format used by Ruby internal methods
# File lib/gorillib/exception/raisers.rb, line 143 def self.undefined_method!(obj) file, line, meth = caller_parts raise self.new(MESSAGE_FMT % [meth, obj, obj.class]) end
Public Instance Methods
Returns true if the object is nil or empty (if applicable)
[].blank? #=> true [1].blank? #=> false [nil].blank? #=> false
@return [TrueClass, FalseClass]
# File lib/gorillib/object/blank.rb, line 11 def blank? nil? || (respond_to?(:empty?) && empty?) end
Coerce a number to lie between min and max.
@example
5.clamp(6, 7) # => 6 5.clamp(6) # => 6 5.clamp(nil, 6) # => 5 5.clamp(nil, 4) # => 4
# File lib/gorillib/numeric/clamp.rb, line 11 def clamp min=nil, max=nil raise ArgumentError, "min must be <= max" if (min && max && (min > max)) return min if min && (self < min) return max if max && (self > max) self end
Add context to the backtrace of exceptions ocurring downstream from caller. This is expecially useful in metaprogramming. Follow the implementation in the example.
@note !! Be sure to rescue the call to this method; few things suck worse than debugging your rescue blocks.
@example
define_method(:cromulate) do |level| begin adjust_cromulance(cromulator, level) rescue StandardError => err ; err.polish("setting cromulance #{level} for #{cromulator}") rescue nil ; raise ; end end
# File lib/gorillib/exception/raisers.rb, line 27 def polish(extra_info) filename, _, method_name = self.class.caller_parts(2) method_name.gsub!(/rescue in /, '') most_recent_line = backtrace.detect{|line| line.include?(filename) && line.include?(method_name) && line.end_with?("'") } most_recent_line.sub!(/'$/, "' for [#{extra_info.to_s[0..300]}]") end
Returns true if the object is NOT nil or empty
[].present? #=> false [1].present? #=> true [nil].present? #=> true
@return [TrueClass, FalseClass]
# File lib/gorillib/object/blank.rb, line 24 def present? not blank? end
Invokes the method identified by the symbol method
, passing it any arguments and/or the block specified, just like the regular Ruby Object#send
does.
Unlike that method however, a NoMethodError
exception will not be raised and nil
will be returned instead, if the receiving object is a nil
object or NilClass
.
If try is called without a method to call, it will yield any given block with the object.
Examples¶ ↑
Without try
@person && @person.name
or
@person ? @person.name : nil
With try
@person.try(:name)
try
also accepts arguments and/or a block, for the method it is trying
Person.try(:find, 1) @people.try(:collect) {|p| p.name}
Without a method argument try will yield to the block unless the receiver is nil.
@person.try { |p| "#{p.first_name} #{p.last_name}" }
# File lib/gorillib/object/try.rb, line 29 def try(*a, &b) if a.empty? && block_given? yield self elsif !a.empty? && !respond_to?(a.first) nil else __send__(*a, &b) end end
Override this in a child if it cannot be dup’ed
@return [Object]
# File lib/gorillib/object/try_dup.rb, line 5 def try_dup self.dup end