Skip to content

Instantly share code, notes, and snippets.

@pocke
Created February 25, 2017 13:58
Show Gist options
  • Save pocke/4d0ee92e061d9d6c6cbd621500d9e6ac to your computer and use it in GitHub Desktop.
Save pocke/4d0ee92e061d9d6c6cbd621500d9e6ac to your computer and use it in GitHub Desktop.
require 'ripper'
require 'pp'
def x(&blk)
blk.call([1,2,3,4,5])
end
code = 'x{|(x, *a, y)| p x; p a; p y }'
eval code
pp Ripper.sexp(code)
@pocke
Copy link
Author

pocke commented Feb 25, 2017

$ ruby a.rb
1
[2, 3, 4]
5
[:program,
 [[:method_add_block,
   [:method_add_arg, [:fcall, [:@ident, "x", [1, 0]]], []],
   [:brace_block,
    [:block_var,
     [:params,
      [[:mlhs_paren,
        [:mlhs_add_star,
         [[:mlhs_paren, [:@ident, "x", [1, 4]]]],
         [:@ident, "a", [1, 8]]]]],
      nil,
      nil,
      nil,
      nil,
      nil,
      nil],
     false],
    [[:command,
      [:@ident, "p", [1, 15]],
      [:args_add_block, [[:var_ref, [:@ident, "x", [1, 17]]]], false]],
     [:command,
      [:@ident, "p", [1, 20]],
      [:args_add_block, [[:var_ref, [:@ident, "a", [1, 22]]]], false]],
     [:command,
      [:@ident, "p", [1, 25]],
      [:args_add_block, [[:var_ref, [:@ident, "y", [1, 27]]]], false]]]]]]]

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