Skip to content

Variable Assignment

Paul Götze edited this page Sep 22, 2015 · 2 revisions

Examples

Local variables:

CodeBreaker.parse("breakfast = 'Ham and eggs'.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]]]}
Clone this wiki locally