N
Navindra Umanee
Hi,
I would like to partially expand a string and then further expand it
later on when more information is available. I am currently using
string interpolation with a reeval method suggested by Bill Kelly.
class String
def reeval(b = TOPLEVEL_BINDING)
eval("<<END_REEVAL\n" + self + "\nEND_REEVAL\n", b).chomp!
end
end
This lets me do stuff like this:
expanded = "one"
delayed = "two"
s = "#{expanded} \#{delayed}" # => "one #{delayed}"
s = s.reeval(binding) # => "one two"
As a more complex example, this is a string I wish to partially expand:
options = [ 10, 20, 30, 40, 50 ]
combobox =<<"END"
...
#{options.map {|n|
%{<option value="#{n}"\#{thread_threshold == n ? ' SELECTED' : ''}>#{n} comments</option>}
}.join("\n")
}
...
END
The idea is to create the combobox and then later insert the SELECTED
part, when I know the value of thread_threshold. Unfortunately, the
above partial expansion is:
<option value="10"#{thread_threshold == n ? ' SELECTED' : ''}>10 comments</option>
<option value="20"#{thread_threshold == n ? ' SELECTED' : ''}>20 comments</option>
<option value="30"#{thread_threshold == n ? ' SELECTED' : ''}>30 comments</option>
<option value="40"#{thread_threshold == n ? ' SELECTED' : ''}>40 comments</option>
<option value="50"#{thread_threshold == n ? ' SELECTED' : ''}>50 comments</option>
This would have been great, except that I have all these values for
'n' that have been left out of the expressions that I want to evaluate
afterwards. That is, I want the partial expansion to be this:
<option value="10"#{thread_threshold == 10 ? ' SELECTED' : ''}>10 comments</option>
<option value="20"#{thread_threshold == 20 ? ' SELECTED' : ''}>20 comments</option>
<option value="30"#{thread_threshold == 30 ? ' SELECTED' : ''}>30 comments</option>
<option value="40"#{thread_threshold == 40 ? ' SELECTED' : ''}>40 comments</option>
<option value="50"#{thread_threshold == 50 ? ' SELECTED' : ''}>50 comments</option>
and later when I reeval the string:
thread_threshold = 50
combobox.reeval(binding)
the result should be:
<option value="10">10 comments</option>
<option value="20">20 comments</option>
<option value="30">30 comments</option>
<option value="40">40 comments</option>
<option value="50" SELECTED>50 comments</option>
Anyone have any ideas for how I can get a correct partial expansion?
I have a lambda/closure feeling about how this should work, but I
can't quite see the solution in the context of strings.
Thanks,
Navin.
I would like to partially expand a string and then further expand it
later on when more information is available. I am currently using
string interpolation with a reeval method suggested by Bill Kelly.
class String
def reeval(b = TOPLEVEL_BINDING)
eval("<<END_REEVAL\n" + self + "\nEND_REEVAL\n", b).chomp!
end
end
This lets me do stuff like this:
expanded = "one"
delayed = "two"
s = "#{expanded} \#{delayed}" # => "one #{delayed}"
s = s.reeval(binding) # => "one two"
As a more complex example, this is a string I wish to partially expand:
options = [ 10, 20, 30, 40, 50 ]
combobox =<<"END"
...
#{options.map {|n|
%{<option value="#{n}"\#{thread_threshold == n ? ' SELECTED' : ''}>#{n} comments</option>}
}.join("\n")
}
...
END
The idea is to create the combobox and then later insert the SELECTED
part, when I know the value of thread_threshold. Unfortunately, the
above partial expansion is:
<option value="10"#{thread_threshold == n ? ' SELECTED' : ''}>10 comments</option>
<option value="20"#{thread_threshold == n ? ' SELECTED' : ''}>20 comments</option>
<option value="30"#{thread_threshold == n ? ' SELECTED' : ''}>30 comments</option>
<option value="40"#{thread_threshold == n ? ' SELECTED' : ''}>40 comments</option>
<option value="50"#{thread_threshold == n ? ' SELECTED' : ''}>50 comments</option>
This would have been great, except that I have all these values for
'n' that have been left out of the expressions that I want to evaluate
afterwards. That is, I want the partial expansion to be this:
<option value="10"#{thread_threshold == 10 ? ' SELECTED' : ''}>10 comments</option>
<option value="20"#{thread_threshold == 20 ? ' SELECTED' : ''}>20 comments</option>
<option value="30"#{thread_threshold == 30 ? ' SELECTED' : ''}>30 comments</option>
<option value="40"#{thread_threshold == 40 ? ' SELECTED' : ''}>40 comments</option>
<option value="50"#{thread_threshold == 50 ? ' SELECTED' : ''}>50 comments</option>
and later when I reeval the string:
thread_threshold = 50
combobox.reeval(binding)
the result should be:
<option value="10">10 comments</option>
<option value="20">20 comments</option>
<option value="30">30 comments</option>
<option value="40">40 comments</option>
<option value="50" SELECTED>50 comments</option>
Anyone have any ideas for how I can get a correct partial expansion?
I have a lambda/closure feeling about how this should work, but I
can't quite see the solution in the context of strings.
Thanks,
Navin.