On May 17, 6:27 am, "Aureliano Calvo" <aurelianoca... / gmail.com>
wrote:
> Now I see that if I can transform all the <"a#{expression}c"> in <"a"
> + (expression) + "c"> in a ruby source code string, I could change
> expression with "a#{expression}c" in "a" + clever_stuff(expression) +
> "c" and use my changed + method in Strings. Is there an easy way to
> manipulate ruby code in ruby to do this?

Can you make life easier by changing to use ERB?
You cannot change Ruby string interpolation in Ruby. You can hack the
source code if you really need to. You can manually change your
strings, or you can (as below) use gsub to change the string before
evaluating.

  def clever_stuff( str )
    str.upcase
  end

  require 'erb'
  def clever_template( str )
    ERB.new( str.gsub( /<%=(.+?)%>/, '<
%=clever_stuff(\1)%>' ) ).result
  end

  name1, name2 = %w|Gavin Kistner|

  str1 = "Hello there, <%=name1%> <%=name2%>! Nice to meet you!"
  puts clever_template( str1 )
  #=> Hello there, GAVIN KISTNER! Nice to meet you!

  str2 = "Hello there, <%=name1 + ' ' + name2%>! Nice to meet you!"
  puts clever_template( str2 )
  #=> Hello there, GAVIN KISTNER! Nice to meet you!