Skip to content

Instantly share code, notes, and snippets.

@benolee
Last active December 11, 2015 04:18
Show Gist options
  • Select an option

  • Save benolee/4543875 to your computer and use it in GitHub Desktop.

Select an option

Save benolee/4543875 to your computer and use it in GitHub Desktop.
method_lambda_proc_returns.rb
DATA.instance_eval { truncate pos.tap { reopen __FILE__, "a+" } }
$stdout = DATA
opts = [nil, nil, nil, trace_instruction: false]
puts "-----> returning from a compiled method"
puts RubyVM::InstructionSequence.new("def _return() return end", *opts).disasm
puts
puts "-----> returning from a lambda"
puts RubyVM::InstructionSequence.new("-> { return }", *opts).disasm
puts
puts "-----> returning from a proc"
puts RubyVM::InstructionSequence.new("proc { return }", *opts).disasm
__END__
-----> returning from a compiled method
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
0000 putspecialobject 1 ( 1)
0002 putspecialobject 2
0004 putobject :_return
0006 putiseq _return
0008 send :"core#define_method", 3, nil, 0, <ic:0>
0014 leave
== disasm: <RubyVM::InstructionSequence:_return@<compiled>>=============
0000 putnil
0001 leave
-----> returning from a lambda
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
0000 putspecialobject 1 ( 1)
0002 send :lambda, 0, block in <compiled>, 8, <ic:0>
0008 leave
== disasm: <RubyVM::InstructionSequence:block in <compiled>@<compiled>>=
== catch table
| catch type: redo st: 0000 ed: 0003 sp: 0000 cont: 0000
| catch type: next st: 0000 ed: 0003 sp: 0000 cont: 0003
|------------------------------------------------------------------------
0000 putnil ( 1)
0001 throw 1
0003 leave
-----> returning from a proc
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
== catch table
| catch type: break st: 0000 ed: 0007 sp: 0000 cont: 0007
|------------------------------------------------------------------------
0000 putself ( 1)
0001 send :proc, 0, block in <compiled>, 8, <ic:0>
0007 leave
== disasm: <RubyVM::InstructionSequence:block in <compiled>@<compiled>>=
== catch table
| catch type: redo st: 0000 ed: 0003 sp: 0000 cont: 0000
| catch type: next st: 0000 ed: 0003 sp: 0000 cont: 0003
|------------------------------------------------------------------------
0000 putnil ( 1)
0001 throw 1
0003 leave
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment