From: Sam Duncan Date: 2012-04-12T13:48:57+09:00 Subject: Re: conditionally use greater than or less than On 12/04/12 16:29, phil rosenstein wrote: > I ended up doing something like this: > > def threshold_compare(value1, value2, operation) > if operation == '>=' > value1>= value2 > elsif operation == '<=' > value1<= value2 > end > end > What about just 1.9.2p290 :001 > def threshold_compare(value1, value2, operator='==') 1.9.2p290 :002?> value1.send(operator.to_sym, value2) 1.9.2p290 :003?> end Additionally you could add a check for valid operators and see if value1 responds to the method if you wanted. Something like if [:==, :>, :<, :>=, :<=].include? operator.to_sym && value1.respond_to? operator.to_sym ... and maybe raise an ArgumentError with a helpful message if the conditions aren't met. You might not need to be paranoid about it depending on your context. You could also 'bake' the valid operators array into a variable somewhere to cut some overhead. Sam