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.
423 424 425 426 427 |
# File 'lib/rubinius/code/ast/literals.rb', line 423 def initialize(line, str, array) @line = line @string = str @array = array end |
Instance Attribute Details
#array ⇒ Object
Returns the value of attribute array.
421 422 423 |
# File 'lib/rubinius/code/ast/literals.rb', line 421 def array @array end |
#options ⇒ Object
Returns the value of attribute options.
421 422 423 |
# File 'lib/rubinius/code/ast/literals.rb', line 421 def 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.
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 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 |
# File 'lib/rubinius/code/ast/literals.rb', line 452 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
508 509 510 511 |
# File 'lib/rubinius/code/ast/literals.rb', line 508 def defined(g) g.push_literal "expression" g.string_dup end |
#sexp_name ⇒ Object
513 514 515 |
# File 'lib/rubinius/code/ast/literals.rb', line 513 def sexp_name :dstr end |
#to_sexp ⇒ Object
517 518 519 |
# File 'lib/rubinius/code/ast/literals.rb', line 517 def to_sexp @array.inject([sexp_name, @string]) { |s, x| s << x.to_sexp } end |