Class: Red::ControlNode::Loop

Inherits:
Red::ControlNode show all
Defined in:
lib/red/nodes/control_nodes.rb

Overview

:nodoc:

Direct Known Subclasses

Until, While

Defined Under Namespace

Classes: Until, While

Instance Method Summary collapse

Methods inherited from String

#%, #*, #+, #<<, #<=>, #==, #=~, #[], #[]=, #capitalize, #capitalize!, #casecmp, #center, #chomp, #chomp!, #chop, #chop!, #concat, #count, #crypt, #delete, #delete!, #downcase, #downcase!, #each, #each_byte, #each_line, #empty?, #eql?, #gsub, #gsub!, #hash, #hex, #include?, #index, #insert, #inspect, #intern, #length, #ljust, #lstrip, #lstrip!, #match, #next, #next!, #oct, #replace, #reverse, #reverse!, #rindex, #rjust, #rstrip, #rstrip!, #scan, #size, #slice, #slice!, #split, #squeeze, #strip, #strip!, #strip_scripts, #sub, #sub!, #succ, #succ!, #sum, #swapcase, #swapcase!, #to_f, #to_i, #to_s, #to_str, #to_sym, #tr, #tr!, #tr_s, #tr_s!, #upcase, #upcase!, #upto

Constructor Details

#initialize(condition_sexp, body_sexp, run_only_if_condition_met, options) ⇒ Loop

:until, expression, | :block, true | false
:while, expression, | :block, true | false


95
96
97
98
99
100
101
102
103
# File 'lib/red/nodes/control_nodes.rb', line 95

def initialize(condition_sexp, body_sexp, run_only_if_condition_met, options)
  condition = (self.is_a?(Until) ? "!$T(%s)" : "$T(%s)") % [condition_sexp.red!(:as_argument => true)]
  body      = body_sexp.red!
  if run_only_if_condition_met
    self << "while(%s){%s;}"   % [condition, body]
  else
    self << "do{%s;}while(%s)" % [body, condition]
  end
end