Skip to content

Instantly share code, notes, and snippets.

@gr33n7007h
Created March 22, 2020 22:39
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 gr33n7007h/fb17b14d1f1ef6e621713a6dd81b4d50 to your computer and use it in GitHub Desktop.
Save gr33n7007h/fb17b14d1f1ef6e621713a6dd81b4d50 to your computer and use it in GitHub Desktop.
>> puts RubyVM::InstructionSequence.compile('-2 ** 2').disasm
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(1,7)> (catch: FALSE)
0000 putobject 2 ( 1)[Li]
0002 putobject 2
0004 opt_send_without_block <calldata!mid:**, argc:1, ARGS_SIMPLE>
0006 opt_send_without_block <calldata!mid:-@, argc:0, ARGS_SIMPLE>
0008 leave
=> nil
>> puts RubyVM::InstructionSequence.compile('-2.itself ** 2').disasm
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(1,14)> (catch: FALSE)
0000 putobject -2 ( 1)[Li]
0002 opt_send_without_block <calldata!mid:itself, argc:0, ARGS_SIMPLE>
0004 putobject 2
0006 opt_send_without_block <calldata!mid:**, argc:1, ARGS_SIMPLE>
0008 leave
=> nil
>> puts RubyVM::InstructionSequence.compile('-(2.itself) ** 2').disasm
== disasm: #<ISeq:<compiled>@<compiled>:1 (1,0)-(1,16)> (catch: FALSE)
0000 putobject 2 ( 1)[Li]
0002 opt_send_without_block <calldata!mid:itself, argc:0, ARGS_SIMPLE>
0004 putobject 2
0006 opt_send_without_block <calldata!mid:**, argc:1, ARGS_SIMPLE>
0008 opt_send_without_block <calldata!mid:-@, argc:0, ARGS_SIMPLE>
0010 leave
=> nil
@havenwood
Copy link

pp Ripper.sexp '-2 ** 2'
[:program,
 [[:unary, :-@, [:binary, [:@int, "2", [1, 1]], :**, [:@int, "2", [1, 6]]]]]]

pp Ripper.sexp '-2.itself ** 2'
[:program,
 [[:binary,
   [:call,
    [:unary, :-@, [:@int, "2", [1, 1]]],
    [:@period, ".", [1, 2]],
    [:@ident, "itself", [1, 3]]],
   :**,
   [:@int, "2", [1, 13]]]]]

pp Ripper.sexp '-(2.itself) ** 2'
[:program,
 [[:unary,
   :-@,
   [:binary,
    [:paren,
     [[:call,
       [:@int, "2", [1, 2]],
       [:@period, ".", [1, 3]],
       [:@ident, "itself", [1, 4]]]]],
    :**,
    [:@int, "2", [1, 15]]]]]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment