class CodeTools::AST::DynamicString

Attributes

array[RW]
options[RW]

Public Class Methods

new(line, str, array) click to toggle source
# File lib/rubinius/code/ast/literals.rb, line 452
def initialize(line, str, array)
  @line = line
  @string = str
  @array = array
end

Public Instance Methods

bytecode(g) click to toggle source

This extensive logic is 100% for optimizing rather ridiculous edge cases for string interpolation and I (brixen) do not think it is worthwhile.

Some examples:

"#{}"
"#{} foo"
"foo #{}"
"#{}#{}"
"#{bar}"

Also, as Zocx pointed out in IRC, the following code is not compatible in Rubinius because we convert an empty evstr into “” directly in Melbourne parse tree to AST processor rather than calling to_s on 'nil'.

def nil.to_s; “hello”; end a = “#{}” # => “hello” in MRI

We also do not handle any case where to_s does not actually return a String instance.

# File lib/rubinius/code/ast/literals.rb, line 481
def bytecode(g)
  pos(g)

  if @string.empty?
    if @array.size == 1
      case x = @array.first
      when StringLiteral
        x.bytecode(g)
      else
        x.bytecode(g)
        # string_build has some auto-conversion logic, use it.
        g.string_build 1
      end
      return
    end

    prefix = false
  else
    prefix = true
    g.push_literal @string
  end

  total = 0
  @array.each do |e|
    case e
    when StringLiteral
      unless e.string.empty?
        g.push_literal e.string
        total += 1
      end
    else
      e.bytecode(g)
      total += 1
    end
  end

  if prefix
    if total == 0
      g.string_dup
      return
    end
    total += 1
  else
    if total == 0
      g.push_literal ""
      g.string_dup
      return
    elsif total == 1
      g.string_build 1
      return
    end
  end

  g.string_build total
end
defined(g) click to toggle source
# File lib/rubinius/code/ast/literals.rb, line 537
def defined(g)
  g.push_literal "expression"
  g.string_dup
end
sexp_name() click to toggle source
# File lib/rubinius/code/ast/literals.rb, line 542
def sexp_name
  :dstr
end
to_sexp() click to toggle source
# File lib/rubinius/code/ast/literals.rb, line 546
def to_sexp
  @array.inject([sexp_name, @string]) { |s, x| s << x.to_sexp }
end