lib/code/node/if.rb in code-ruby-0.10.4 vs lib/code/node/if.rb in code-ruby-0.11.0
- old
+ new
@@ -10,36 +10,37 @@
class Else < Node
attr_reader :operator, :statement, :body
def initialize(parsed)
- @operator = parsed.delete(:operator)
- @body = Node::Code.new(parsed.delete(:body))
+ return if parsed.blank?
+ @operator = parsed.delete(:operator).presence
+ @body = Node::Code.new(parsed.delete(:body).presence)
- return unless parsed.key?(:statement)
-
- @statement = Node::Statement.new(parsed.delete(:statement))
+ if parsed.key?(:statement)
+ @statement = Node::Statement.new(parsed.delete(:statement).presence)
+ end
end
end
def initialize(parsed)
- @first_operator = parsed.delete(:first_operator)
- @first_statement = Node::Statement.new(parsed.delete(:first_statement))
- @first_body = Node::Code.new(parsed.delete(:first_body))
- @elses =
- parsed.delete(:elses) { [] }.map { |elses| Node::If::Else.new(elses) }
- super(parsed)
+ return if parsed.blank?
+ @first_operator = parsed.delete(:first_operator).presence
+ @first_statement = Node::Statement.new(parsed.delete(:first_statement).presence)
+ @first_body = Node::Code.new(parsed.delete(:first_body).presence)
+ @elses = (parsed.delete(:elses).presence || [])
+ @elses.map! { |elses| Node::If::Else.new(elses) }
end
def evaluate(**args)
if @first_operator == IF_KEYWORD &&
@first_statement.evaluate(**args).truthy?
@first_body.evaluate(**args)
elsif @first_operator == UNLESS_KEYWORD &&
@first_statement.evaluate(**args).falsy?
@first_body.evaluate(**args)
else
- @elses.each do |elses|
+ (@elses || []).each do |elses|
if elses.operator == ELSIF_KEYWORD &&
elses.statement.evaluate(**args).truthy?
return elses.body.evaluate(**args)
elsif elses.operator == IF_KEYWORD &&
elses.statement.evaluate(**args).truthy?