Class: Rubinius::ToolSet.current::TS::AST::DynamicString

Inherits:
StringLiteral show all
Defined in:
lib/rubinius/ast/literals.rb

Direct Known Subclasses

DynamicExecuteString, DynamicRegex, DynamicSymbol

Instance Attribute Summary collapse

Attributes inherited from StringLiteral

#string

Attributes inherited from Node

#line

Instance Method Summary collapse

Methods inherited from Node

#ascii_graph, #attributes, #children, match_arguments?, match_send?, #new_block_generator, #new_generator, #node_name, #or_bytecode, #pos, #set_child, transform, #transform, transform_comment, transform_kind, transform_kind=, transform_name, #value_defined, #visit, #walk

Constructor Details

#initialize(line, str, array) ⇒ DynamicString

Returns a new instance of DynamicString.



363
364
365
366
367
# File 'lib/rubinius/ast/literals.rb', line 363

def initialize(line, str, array)
  @line = line
  @string = str
  @array = array
end

Instance Attribute Details

#arrayObject

Returns the value of attribute array.



361
362
363
# File 'lib/rubinius/ast/literals.rb', line 361

def array
  @array
end

#optionsObject

Returns the value of attribute options.



361
362
363
# File 'lib/rubinius/ast/literals.rb', line 361

def options
  @options
end

Instance Method Details

#bytecode(g) ⇒ Object

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.



392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
# File 'lib/rubinius/ast/literals.rb', line 392

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) ⇒ Object



448
449
450
451
# File 'lib/rubinius/ast/literals.rb', line 448

def defined(g)
  g.push_literal "expression"
  g.string_dup
end

#sexp_nameObject



453
454
455
# File 'lib/rubinius/ast/literals.rb', line 453

def sexp_name
  :dstr
end

#to_sexpObject



457
458
459
# File 'lib/rubinius/ast/literals.rb', line 457

def to_sexp
  @array.inject([sexp_name, @string]) { |s, x| s << x.to_sexp }
end