On Sat, Nov 17, 2012 at 2:50 PM, Philip Rhoades <[email protected]> wrote:
> People,
>
> Thanks to the people who responded - I looked at everything but nothing
> really suited what I was trying to do but see below for some progress that I
> made and another question:
> I found that I could use sequential "ifs" instead of nested "ifs" by using
> "exit" statements in each "if" block - the script is processing incoming
> mails and can exit when a mail has been satisfactorily classified.
Ah, now we are cooking! That looks like a proper use case for case.
> I still
> have something like this:
>
>
> lots of code
> if block - small
> lots of code
> if block - small
> if block - large
> if block - small
> if block - large
> .
> .
> etc
For a start you could replace that with either
case email
when /foo/
asdsdds
when /bar/
adioaiosdipaosdpoasdopad ads
when /something_else/
method_which_contains_lots_of_code(email)
else
raise "No category" # or whatever
end
Or, for more complex checks
case
when /foo/ =~ email && email.length > 1000
d adasdsd ada
when email.urgent?
adslkasda
else
asdsd
end
> What I want is to be able to see the whole logic of the program in about 25
> lines (ie one screenfull of code - ie all the "if" statements) - I could
> probably use standard methods for the repetitive small "if" blocks but for
> the large "lots of code" and "if block - large" I definitely can't. Is it
> possible that a method can be forced to NOT use local variables? - then I
> could replace the large blocks with these sorts of methods.
I am not sure I understand what you mean here.
Anyway, if you are categorizing you could use a similar approach to botp's:
CATEGORIES = [
lambda {|email| email.size > 1000 and :large},
lambda {|email| email.size <= 1000 and :small},
]
def categorize(email)
CATEGORIES.each {|c| x = c[email] and return x}
nil
end
Kind regards
robert
--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/
-- You received this message because you are subscribed to the Google Groups
ruby-talk-google group. To post to this group, send email to
[email protected]. To unsubscribe from this group, send email
to [email protected]. For more options, visit this
group at https://groups.google.com/d/forum/ruby-talk-google?hl=en