Created
February 3, 2022 09:56
-
-
Save baweaver/9b5e40cf8edb194c0bca35c87a99eafb to your computer and use it in GitHub Desktop.
Proc to AST applied to potential foundation for SQL hackery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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