B
Bob Nadler
Of course, the example was made up to demonstrate the ends and not real
world code. I would not write it in this way, either...
This one is from existing code:
=A0module ABC
=A0 =A0# ...
=A0 =A0def abc
=A0 =A0 =A0x.each do
=A0 =A0 =A0 =A0if x
=A0 =A0 =A0 =A0 =A0case lorem
=A0 =A0 =A0 =A0 =A0when :ipsum
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0when :ipsum
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0when :ipsum
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0when :ipsum
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0else
=A0 =A0 =A0 =A0 =A0 =A0# ...
=A0 =A0 =A0 =A0 =A0end
=A0 =A0 =A0 =A0end
=A0 =A0 =A0end
=A0 =A0end
=A0end
My general rule of thumb is to put long case statements (if I can't avoid
using them) into their own separate method. So I would refactor the code to=
:
module ABC
def abc
x.each do
some_method_name_that_describes_what_case_statement_does(lorem) if x
end
end
def some_method_name_that_describes_what_case_statement_does(value)
case lorem
when :ipsum
# ...
when :ipsum
# ...
when :ipsum
# ...
when :ipsum
# ...
else
# ...
end
end
end
As an aside, take a look at Refactoring: Ruby Edition[1]. It covers ways of
dealing with long methods, case statements, and other situations that
cause difficulties trying
to match up "end"'s.
[1] http://www.amazon.com/Refactoring-Ruby-Jay-Fields/dp/0321603508