On 17.05.2007 14:27, Aureliano Calvo wrote:
>> > I'm trying to do some metaprogramming and I need to apply some
>> > operation to all the strings that are interpolated. But I can't change
>> > the interpolation. In the example, I would like that "hi, #{name}" to
>> > evaluate to "hi, AURE" instead of "hi, Aure". I know that inside the
>> > #{} "operator" I can put any ruby code, so "hi, #{name.capitalize}"
>> > would evaluate to what I want.
>> >
>> > But I want it to execute code that I DON'T write there. Ideally, there
>> > should be a method hook or something to change the way the
>> > interpolation works. But I couldn't find it :(.  May be a different
>> > example may be more clear. How can I do to write to a file all the
>> > strings generated via interpolations (id est, all the strings that are
>> > generated evaluating the different #{} "operators" in a program)?
>>
>> Frankly, it has not become clear to me what you are up to.  Can you
>> maybe just state which problem you are trying to solve?
> 
> NDAs are a bitch. I can't state the exact problem (bah! I can, but I
> could be fired, in a trial, and I also might inhibit my coworkers to
> publish a paper with their findings).

You can tell us - we won't tell anybody else. ;-)

> I'm working in a research
> project and I need to do some "clever stuff" (that's the thing I can't
> disclose) to all the Strings. This thing is "different" depending on
> how strings are composed. All the ways I know for string composition
> but string interpolation (<<, +, concat, gsub, etc.) can be overridden
> redefining methods in the string class. I'm looking for a way to
> intercept the string expansion to do my thing. Does ruby internally
> call some overridable method to compose the strings used in a
> interpolation?

Likely but also likely not accessible to pure Ruby code.  But in your 
case (research project) it might be ok to hack the interpreter if you 
need to catch all string interpolations.  Did you look into this yet?

> 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?

You could even change all "a#{b}c" into "a#{your_magic b}c" which is 
considerably easier to do than a complete parse.  But when I think about 
it it may break for some b's.  Whether that's ok or not, I don't know.

Kind regards

	robert