Sha256: 4f9176eee29656d3495c25ab2091cfde065b8d60066ed8a7bddbd56c56c7c951

Contents?: true

Size: 1.64 KB

Versions: 23

Compression:

Stored size: 1.64 KB

Contents

Kernel.class_eval do
  def respond_to sym, *args
    return nil if not respond_to? sym
    send sym, *args
  end
  
  #  def _ &b
  #    raise "Block isn't provided!" unless b
  #    return b 
  #  end
  
  def singleton_class(&block)
    if block_given?
      (class << self; self; end).class_eval(&block)
    self
    else
      (class << self; self; end)
    end
  end  
  alias_method :metaclass, :singleton_class
  alias_method :metaclass_eval, :singleton_class
  
  # Removes the namespace (exact match and underscored) of given class (or self) from stacktrace
  def raise_without_self *args
    error, message, klasses = nil
    if args.size == 1
      error = RuntimeError
      message = args[0]
      klasses = [self]
    elsif args.size == 2
      message, klasses = args
      error = RuntimeError
      
      klasses = Array(klasses)
      klasses << self
    elsif args.size == 3
      error, message, klasses = args
      
      klasses = Array(klasses)
      klasses << self
    else
      raise RuntimeError, "Invalid arguments!", caller
    end
    
    klasses.collect!{|c| (c.class == Class or c.class == Module or c.class == String) ? c : c.class}
    
    # obtaining the namespace of each class
    klasses.collect! do |c|
      if c.respond_to? :namespace
        c = c.namespace while c.namespace
        c
      else
        c
      end
    end
    
    # building regexp
    skip = []
    klasses.each do |c|
      skip.push(/\/#{c.to_s}/) # exact match
      skip.push(/\/#{c.to_s.underscore}/) # underscored
    end

    # cleaning stacktrace
    stack = caller.select{|path| !skip.any?{|re| re =~ path}}
    
    raise error, message, stack
  end
end

Version data entries

23 entries across 23 versions & 2 rubygems

Version Path
ruby-ext-0.4.4 lib/ruby_ext/kernel.rb
ruby-ext-0.4.3 lib/ruby_ext/kernel.rb
ruby-ext-0.4.2 lib/ruby_ext/kernel.rb