Class: Red::LiteralNode::Multiline

Inherits:
Red::LiteralNode show all
Defined in:
lib/red/nodes/literal_nodes.rb

Overview

:nodoc:

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(*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