module Kernel

Public Instance Methods

_backtrace_from_continuation(&block) click to toggle source
# File lib/r_kit/backtrace/kernel_extend.rb, line 21
def _backtrace_from_continuation &block
  cc = nil
  block ||= ->(obj){ obj != self }

  tracer = TracePoint.trace(:return) do |tp|
    if tp.self != self && block.call(tp.self)
      tracer.disable
      cc.call tp.self
    end
  end

  backtrace = callcc { |cont| cc = cont }
  backtrace unless backtrace.is_a? Continuation
end
Also aliased as: backtrace
_backtrace_from_ruby_vm(&block) click to toggle source
# File lib/r_kit/backtrace/kernel_extend.rb, line 3
def _backtrace_from_ruby_vm &block
  stack_level = 0
  block ||= ->(obj){ obj != self }

  RubyVM::DebugInspector.open do |inspector|
    loop do
      stack_binding = begin
        inspector.frame_binding(stack_level += 1)
      rescue ArgumentError
        nil
      end

      obj = stack_binding.try :eval, 'self'
      return obj if obj != self && block.call(obj)
    end
  end
end
Also aliased as: backtrace
backtrace(&block)
conditionnal_statement(**options) click to toggle source

TODO: check for “.present?/.presence” when ‘send(method_name)’, in case of non nil but empty TODO: also, allow to send objects in ‘if/unless’, and only send if symbol, or exec if proc

# File lib/r_kit/utility/kernel_extend.rb, line 25
def conditionnal_statement **options
  options.slice(:if, :unless).reduce(true) do |continue, (statement, method_name)|
    continue && send(method_name).tap do |closure|
      case statement
      when :if
        !!closure
      when :unless
        !closure
      end
    end
  end
end
dont_respond_to?(*args, &block) click to toggle source
# File lib/r_kit/utility/kernel_extend.rb, line 61
def dont_respond_to? *args, &block
  !respond_to? *args, &block
end
running_script() click to toggle source
# File lib/r_kit/utility/kernel_extend.rb, line 3
def running_script
  "#{ File.basename($0) } #{ ARGV.join " " }"
end
running_script?(script) click to toggle source
# File lib/r_kit/utility/kernel_extend.rb, line 7
def running_script? script
  Regexp.new(script) =~ running_script
end
shadow(**shadows, &block) click to toggle source
# File lib/r_kit/utility/kernel_extend.rb, line 40
def shadow **shadows, &block
  saved_state = shadows.keys.reduce({}) do |saved_state, name|
    saved_state[name] = instance_variable_get name.ivar
    saved_state
  end

  shadows.each do |name, value|
    instance_variable_set name.ivar, value
  end

  closure = block.call(self)

  shadows.keys.each do |name|
    instance_variable_set name.ivar, saved_state[name]
  end

  closure
end
then(**options, &block) click to toggle source

TODO: think about a ‘else’ method => x.then{}.else{} it could be done, when the ‘then’ return ‘nil’, we define an instance var in the singleton (possible on nil ?) then reuse that into the else, as an arg to the block

# File lib/r_kit/utility/kernel_extend.rb, line 15
def then **options, &block
  if self && conditionnal_statement(options)
    block.call(self)
  else
    self
  end
end