class Object
Constants
- Binary
- Double
- EpochTime
- Guid
- Hostname
- IntTime
- Long
- MESSAGE_FMT
- Url
Public Class Methods
# File lib/gorillib/core_ext/exception.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/core_ext/exception.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/core_ext/exception.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/core_ext/exception.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
Parses the time but never fails. Return value is always in the UTC time zone.
A flattened datetime – a 14-digit YYYYmmddHHMMMSS – is fixed to the UTC time zone by parsing it as YYYYmmddHHMMMSSZ <- ‘Z’ at end
# File lib/gorillib/core_ext/datetime.rb, line 9 def self.parse_safely dt return nil if dt.nil? || (dt.respond_to?(:empty) && dt.empty?) begin case when dt.is_a?(Time) then dt.utc when (dt.to_s =~ /\A\d{14}\z/) then parse(dt.to_s+'Z', true) else parse(dt.to_s, true).utc end rescue StandardError => err warn "Can't parse a #{self} from #{dt.inspect}" warn err return nil end end
Raise an error with the same format used by Ruby internal methods
# File lib/gorillib/core_ext/exception.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
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/core_ext/exception.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
# File lib/gorillib/core_ext/module.rb, line 4 def remove_possible_method(method) if method_defined?(method) || private_method_defined?(method) remove_method(method) end rescue NameError end
Override this in a child if it cannot be dup’ed
@return [Object]
# File lib/gorillib/core_ext/object.rb, line 5 def try_dup self.dup end