Class: Red::ControlNode::ForNode

Inherits:
Object
  • Object
show all
Defined in:
lib/red/control_nodes.rb

Overview

:nodoc:

Instance Method Summary collapse

Constructor Details

#initialize(source, iterator, body) ⇒ ForNode

Returns a new instance of ForNode.



34
35
36
37
# File 'lib/red/control_nodes.rb', line 34

def initialize(source, iterator, body)
  @properties_loop = (iterator.last == :property)
  @source, @iterator, @body = [source, iterator.last, body].build_nodes
end

Instance Method Details

#compile_node(options = {}) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'lib/red/control_nodes.rb', line 39

def compile_node(options = {})
  source = @source.compile_node(:as_argument => true)
  iterator = @iterator.compile_node
  body = @body.compile_node
  if @properties_loop
    "for (var property in %s) { %s; }" % [source, body]
  else
    "for (var %s = 0; %s < %s.length; %s++) { %s; }" % [iterator, iterator, source, iterator, body]
  end
end