Skip to content

Instantly share code, notes, and snippets.

@baweaver
Created February 3, 2022 09:56
Show Gist options
  • Save baweaver/9b5e40cf8edb194c0bca35c87a99eafb to your computer and use it in GitHub Desktop.
Save baweaver/9b5e40cf8edb194c0bca35c87a99eafb to your computer and use it in GitHub Desktop.
Proc to AST applied to potential foundation for SQL hackery
require 'proc_to_ast'
def where(&fn)
ast = fn.to_ast
source = fn.to_source
pp ast:, source:
end
where { age > 18 && occupation.downcase == 'plumber' }
# OUTPUT:
{:ast=>
s(:block,
s(:send, nil, :where),
s(:args),
s(:and,
s(:send,
s(:send, nil, :age), :>,
s(:int, 18)),
s(:send,
s(:send,
s(:send, nil, :occupation), :downcase), :==,
s(:str, "plumber")))),
:source=>"where {\n" + " age > 18 && occupation.downcase == \"plumber\"\n" + "}"}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment