Module: Kernel
- Defined in:
- 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
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.
- #dont_respond_to?(*args, &block) ⇒ Boolean
- #running_script ⇒ Object
- #running_script?(script) ⇒ Boolean
- #shadow(**shadows, &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
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
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/r_kit/utility/kernel_extend.rb', line 25 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
61 62 63 |
# File 'lib/r_kit/utility/kernel_extend.rb', line 61 def dont_respond_to? *args, &block !respond_to? *args, &block 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(**shadows, &block) ⇒ Object
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# 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) ⇒ 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 |