Class: CodeTools::AST::DynamicString
- Inherits:
-
StringLiteral
- Object
- Node
- StringLiteral
- CodeTools::AST::DynamicString
- Defined in:
- lib/rubinius/code/ast/literals.rb
Direct Known Subclasses
Instance Attribute Summary collapse
-
#array ⇒ Object
Returns the value of attribute array.
-
#options ⇒ Object
Returns the value of attribute options.
Attributes inherited from StringLiteral
Attributes inherited from Node
Instance Method Summary collapse
-
#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.
- #defined(g) ⇒ Object
-
#initialize(line, str, array) ⇒ DynamicString
constructor
A new instance of DynamicString.
- #sexp_name ⇒ Object
- #to_sexp ⇒ Object
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.
453 454 455 456 457 |
# File 'lib/rubinius/code/ast/literals.rb', line 453 def initialize(line, str, array) @line = line @string = str @array = array end |
Instance Attribute Details
#array ⇒ Object
Returns the value of attribute array.
451 452 453 |
# File 'lib/rubinius/code/ast/literals.rb', line 451 def array @array end |
#options ⇒ Object
Returns the value of attribute options.
451 452 453 |
# File 'lib/rubinius/code/ast/literals.rb', line 451 def @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 #{}"
"#{}#{}"
"#{}"
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.
482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/rubinius/code/ast/literals.rb', line 482 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
538 539 540 541 |
# File 'lib/rubinius/code/ast/literals.rb', line 538 def defined(g) g.push_literal "expression" g.string_dup end |
#sexp_name ⇒ Object
543 544 545 |
# File 'lib/rubinius/code/ast/literals.rb', line 543 def sexp_name :dstr end |
#to_sexp ⇒ Object
547 548 549 |
# File 'lib/rubinius/code/ast/literals.rb', line 547 def to_sexp @array.inject([sexp_name, @string]) { |s, x| s << x.to_sexp } end |