Method: Red::LiteralNode::Multiline#initialize

Defined in:
lib/red/nodes/literal_nodes.rb

#initialize(*expression_sexps) ⇒ Multiline

:block, expression, expression, …


25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/red/nodes/literal_nodes.rb', line 25

def initialize(*expression_sexps)
  # force_return: changes final expression to "return <expression>",
  #   unless there is already return anywhere inside <expression>.
  # as_argument: duplicates :force_return and adds "function() {
  #   <multiline block>; }()" wrapper to the entire block.
  options = expression_sexps.pop
  if options[:as_argument] || options[:as_assignment] || options.delete(:force_return) && expression_sexps.last.is_a?(::Array) && !expression_sexps.last.is_sexp?(:rescue, :ensure, :begin) && (expression_sexps.last.first == :iter ? true : !expression_sexps.last.flatten.include?(:return))
    returner = "return %s" % [expression_sexps.pop.red!(:as_argument => true)]
  end
  string = options[:as_argument] || options[:as_assignment] ? "function(){%s;}.m$(this)()" : "%s"
  lines = (expression_sexps.map {|line| line.red!(options)} + [returner]).compact.reject {|x| x == '' }.join(";#{options[:as_class_eval] ? "\n  " : ''}")
  self << string % [lines]
end