-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Paul Götze edited this page Sep 21, 2015
·
7 revisions
...is breaking a Ruby code snippet into a sequence of classes and their connecting methods and statements.
It uses Parser under the hood to break statements into its types and statements based on Ruby Abstract Syntax Tree (AST) nodes.
Local variables:
CodeBreaker.parse("breakfast = 'Ham and backon'.upcase + 'Nom, nom!'")
# => {:lvasgn=>[:breakfast, [String, :upcase, :+, String]]} Class variables:
CodeBreaker.parse("@@breakfast = 'Ham and eggs'.upcase + 'Nom, nom!'")
# => {:cvasgn=>[:@@breakfast, [String, :upcase, :+, String]]}Instance variables:
CodeBreaker.parse("@breakfast = 'Ham and eggs'.upcase + 'Nom, nom!'")
# => {:ivasgn=>[:@breakfast, [String, :upcase, :+, String]]}Global variables:
CodeBreaker.parse("$breakfast = 'Ham and eggs'.upcase + 'Nom, nom!'")
# => {:gvasgn=>[:$breakfast, [String, :upcase, :+, String]]} Constants:
CodeBreaker.parse("BEST_BREAKFAST = 'Ham and eggs'.freeze")
# => {:casgn=>[:BEST_BREAKFAST, [String, :freeze]]}Multiple variables:
CodeBreaker.parse("food, drink = 'Bread', false")
# => {:masgn=>{[:food, :drink]=>[String, FalseClass]}}Operation assignment:
CodeBreaker.parse("count += 1")
# => {:op_asgn=>[{:lvasgn=>[:count]}, :+, Fixnum]}Or assignment:
CodeBreaker.parse("@gem_name ||= 'code_breaker'")
# => {:or_asgn=>[{:ivasgn=>[:@gem_name]}, String]}And assignment:
CodeBreaker.parse("valid &&= (gem_name == 'code_breaker')")
# => {:and_asgn=>[{:lvasgn=>[:valid]}, [[:gem_name, :==, String]]]}