Creating function name from variables

I have a situation where I have a number of fields and a rating for the
field. So, in a DRY way, this works fine:
<%= [‘abc’, ‘def’, ‘klm’].each do |e| %>

<%= @result[e] %> <%= @result[e+'rating'] %> <% end %>

Now, I want to call a custom format function that translates the rating
into things like ‘Good’, ‘Bad’, ‘Ugly’ and also sends back the value of
the result[e] above into the correct format. So, I have helpers for
each of the fields.

FLAGMEAN = [ " “, “!”, “+”, “-”, “*” ] unless
const_defined?(“FLAGMEAN”)
def format_abc(val, flag)
a = sprintf (”%.1f",(val/ 10.0))
b = FLAGMEAN[flag]
return a, b
end

def format_def(val, flag)
a = sprintf ("%.1f",(val/ 100.0))

end

These need to be separate function since the division factors, etc.
depend on the actual field (whether it is ‘abc’, ‘def’, ‘klm’, …) and
so I’m looking for a DRY way to call these by still keeping what I did
above. I want to do:
<%= [‘abc’, ‘def’, ‘klm’].each do |e| %>

<% a, b = format_{something_that_depends_on_e} (@result[e], @result[e+'rating']) %> ### <-- what do I use? <%= a %> <%= b %> <% end %>

Any ideas? Or can I not use something to construct the name… I tried
a variety of combinations involving: format_ + ‘e’, format + e, format +
#e, format + #{e} and other inane ways of getting it right. But sadly,
I have to give up and ask for help.

Thanks,
Mohit.

<%= [‘abc’, ‘def’, ‘klm’].each do |e| %>

<% a, b = format_{something_that_depends_on_e} (@result[e], @result[e+'rating']) %> ### <-- what do I use? <%= a %> <%= b %> <% end %>

You could try:

<%= send(“format_#{e}”, @result[e], @result[e+‘rating’]) %>

Max

Max M. wrote:

<%= send(“format_#{e}”, @result[e], @result[e+‘rating’]) %>

Max

Thanks Max,

This is un#{expletive}believable!! It makes for such an elegant piece
of code… thanks for the help!

Cheers
Mohit.