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| %>
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| %>
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.