Module: Kernel
- Defined in:
- lib/r_kit/grid/kernel_extend.rb,
lib/r_kit/utility/kernel_extend.rb,
lib/r_kit/backtrace/kernel_extend.rb
Instance Method Summary collapse
- #_backtrace_from_continuation(&block) ⇒ Object (also: #backtrace)
- #_backtrace_from_ruby_vm(&block) ⇒ Object
- #conditionnal_statement(**options) ⇒ Object
- #dont_respond_to?(*args, &block) ⇒ Boolean
- #Grid(object, &block) ⇒ Object
- #running_script ⇒ Object
- #running_script?(script) ⇒ Boolean
- #shadow(*names, &block) ⇒ Object
-
#then(**options, &block) ⇒ Object
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.
Instance Method Details
#_backtrace_from_continuation(&block) ⇒ Object Also known as: backtrace
21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
# 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 |
#_backtrace_from_ruby_vm(&block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# 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 |
#conditionnal_statement(**options) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/r_kit/utility/kernel_extend.rb', line 23 def conditionnal_statement ** .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) ⇒ Boolean
55 56 57 |
# File 'lib/r_kit/utility/kernel_extend.rb', line 55 def dont_respond_to? *args, &block !respond_to? *args, &block end |
#Grid(object, &block) ⇒ Object
3 4 5 6 7 8 9 10 11 12 13 |
# File 'lib/r_kit/grid/kernel_extend.rb', line 3 def Grid object, &block if object.kind_of? RKit::Grid::Base object elsif object.respond_to? :to_grid object.to_grid &block else RKit::Grid::Base::Grid.new Array.wrap(object), &block end end |
#running_script ⇒ Object
3 4 5 |
# File 'lib/r_kit/utility/kernel_extend.rb', line 3 def running_script "#{ File.basename($0) } #{ ARGV.join " " }" end |
#running_script?(script) ⇒ Boolean
7 8 9 |
# File 'lib/r_kit/utility/kernel_extend.rb', line 7 def running_script? script Regexp.new(script) =~ running_script end |
#shadow(*names, &block) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/r_kit/utility/kernel_extend.rb', line 38 def shadow *names, &block saved_state = names.reduce({}) do |saved_state, name| saved_state[name] = instance_variable_get name.ivar saved_state end closure = block.call(self) names.each do |name| instance_variable_set name.ivar, saved_state[name] end closure end |
#then(**options, &block) ⇒ Object
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
15 16 17 18 19 20 21 |
# File 'lib/r_kit/utility/kernel_extend.rb', line 15 def then **, &block if self && conditionnal_statement() block.call(self) else self end end |