Todd Benson
5/6/2008 2:27:00 PM
On Tue, May 6, 2008 at 4:55 AM, Tim Booher <ads@theboohers.org> wrote:
> This function should work, but when I wrote it, I had the feeling that
> there would be a much better way to code this function. In particular, I
> don't let creating the discount_out variable. anyone want to give me a
> nudge in the right direction?
>
> def discount(discount_category)
> # 0 => no discount
> # 1 => friend
> # 2 => vet
> # 3 => super-vet
> case discount_category
> when 1
> discount_out = 40
> when 2
> discount_out = 100
> when 3
> discount_out = 160
> else
> discount_out = 0
> end
> discount_out
> end
>
> best,
>
> tim
You already have a hash structure...
def discount category
#I'll set up the hash for demonstration
#You would probably use class instance
#variables instead, or a const for your
#id to category mapping outside of the def
(h = Hash[1, 40, 2, 100, 3, 160]).default = 0
#grab the category
h[category]
end
...in which case you could do away with the def altogether depending
on what you're trying to do.
Todd