OK, obviously, I was too lazy. I thought there should be a cleaner way to do this, Ruby is the best programming language and all;) To evaluate a string like
a = '#{1+1} some text #{big_class.inspect}'
only when necessary, I did not find a better way than to go through the line, and eval all "# {}" collided:
str = "" "#{b}\#{}".scan( /(.*?)(#\{[^\}]*\})/ ){ str += $1 str += eval( $2[2..-2] ).to_s }
If you are not clear, you can get rid of the temporary variable str :
"#{b}\#{}".scan( /(.*?)(#\{[^\}]*\})/ ).collect{|c| c[0] + eval( c[1][2..-2] ).to_s }.join
The String.scan method goes through each block "# {}", as there can be several evaluating it ( 2 ..- 2 cuts out "# {" and "}") and place it along with the rest of the string.
For a rectangle in a line that does not end with a '# {}' block, an empty block is added to be sure.
But well, after several years working in Ruby, it still seems awkward and C-ish. Maybe it's time to learn a new language!
source share