On Mon, Feb 18, 2013 at 2:43 PM, Joel Pearson <[email protected]> wrote:

> I do use switches occasionally, here's one example where I think it's
> justified (from my older Excel_Sheet<Array class):
>
> def filter( header, regex, switch=true )
>   fail ArgumentError, "#{regex} is not valid Regexp" unless regex.class
> == Regexp
>   idx = self[0].index header
>   fail ArgumentError,  "#{header} is not a valid header" if idx.nil?
>   operator = ( switch ? :=~ : :!~ )
>   Excel_Sheet.new skip_headers { |xl| xl.select { |ar| ar[idx].send(
> operator, regex ) } }
> end
>
> Mostly I just did that because I was learning how to use symbols, but it
> makes the Regex more flexible with the minimum amount of repetition or
> long-winded "if" statements.

I think there are better solutions - in order of increasing quality:

1. Rename the parameter from "switch" to "negate".
2. Pass the operator symbol directly.
3. Add a method Regexp#negate which will return an instance which has
matching logic reversed and remove the parameter.
4. yield ar[idx] (i.e. delegate the decision to the block passed in)

I am still not fond of the #skip_headers approach.  As far as I can
see you want to copy the sheet into a new one and include only cells
whose  content matches a regular expression (or maybe other filter
criteria).  I don't see the rest of your current version of the
implementation but the line with self[0] somehow looks strange.

Kind regards

robert

-- 
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

-- 
[email protected] | 
https://groups.google.com/d/forum/ruby-talk-google?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"ruby-talk-google" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to