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| %>
<tr><td><%= @result[e] %></td>
<td> <%= @result[e+''rating'']
%></td>
</tr>
<% 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| %>
<tr><% a, b = format_{something_that_depends_on_e} (@result[e],
@result[e+''rating'']) %> ### <-- what do I use?
<td><%= a %></td>
<td> <%= b %></td>
</tr>
<% 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.