Skip to content

Instantly share code, notes, and snippets.

@steveklabnik
Created August 10, 2016 19:04
Embed
What would you like to do?
for +=
irb(main):001:0> puts RubyVM::InstructionSequence.compile("x += 1").disasm
== disasm: #<ISeq:<compiled>@<compiled>>================================
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] x
0000 trace 1 ( 1)
0002 getlocal_OP__WC__0 2
0004 putobject_OP_INT2FIX_O_1_C_
0005 opt_plus <callinfo!mid:+, argc:1, ARGS_SIMPLE>, <callcache>
0008 dup
0009 setlocal_OP__WC__0 2
0011 leave
=> nil
same as -=
irb(main):002:0> puts RubyVM::InstructionSequence.compile("x -= 1").disasm
== disasm: #<ISeq:<compiled>@<compiled>>================================
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] x
0000 trace 1 ( 1)
0002 getlocal_OP__WC__0 2
0004 putobject_OP_INT2FIX_O_1_C_
0005 opt_minus <callinfo!mid:-, argc:1, ARGS_SIMPLE>, <callcache>
0008 dup
0009 setlocal_OP__WC__0 2
0011 leave
=> nil
irb(main):004:0>
but different for ||=
irb(main):003:0> puts RubyVM::InstructionSequence.compile("x ||= 1").disasm
== disasm: #<ISeq:<compiled>@<compiled>>================================
local table (size: 2, argc: 0 [opts: 0, rest: -1, post: 0, block: -1, kw: -1@-1, kwrest: -1])
[ 2] x
0000 trace 1 ( 1)
0002 getlocal_OP__WC__0 2
0004 dup
0005 branchif 12
0007 pop
0008 putobject_OP_INT2FIX_O_1_C_
0009 dup
0010 setlocal_OP__WC__0 2
0012 leave
=> nil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment