Skip to content

Instantly share code, notes, and snippets.

@ashmoran
Created November 8, 2013 16:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ashmoran/7373241 to your computer and use it in GitHub Desktop.
Save ashmoran/7373241 to your computer and use it in GitHub Desktop.
Disassembled YARV versions of various nil checks in Ruby 2
[
"true unless nil.nil?",
"true if nil != nil",
"true if !!nil",
"true unless nil"
].each do |source|
puts "====="
puts source
puts
puts RubyVM::InstructionSequence.compile(source).disasm
puts "====="
end
=====
true unless nil.nil?
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
0000 trace 1 ( 1)
0002 putnil
0003 opt_send_simple <callinfo!mid:nil?, argc:0, ARGS_SKIP>
0005 branchunless 10
0007 putnil
0008 leave
0009 pop
0010 putobject true
0012 leave
=====
=====
true if nil != nil
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
0000 trace 1 ( 1)
0002 putnil
0003 putnil
0004 opt_neq <callinfo!mid:!=, argc:1, ARGS_SKIP>, <callinfo!mid:==, argc:1, ARGS_SKIP>
0007 branchunless 13
0009 putobject true
0011 leave
0012 pop
0013 putnil
0014 leave
=====
=====
true if !!nil
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
0000 trace 1 ( 1)
0002 putnil
0003 opt_not <callinfo!mid:!, argc:0, ARGS_SKIP>
0005 opt_not <callinfo!mid:!, argc:0, ARGS_SKIP>
0007 branchunless 13
0009 putobject true
0011 leave
0012 pop
0013 putnil
0014 leave
=====
=====
true unless nil
== disasm: <RubyVM::InstructionSequence:<compiled>@<compiled>>==========
0000 trace 1 ( 1)
0002 jump 7
0004 putnil
0005 leave
0006 pop
0007 putobject true
0009 leave
=====
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment