Hi --
this is not meant as a flame, but this reminds me of the
"write only" code I used to see when I programmed in C.
If given the choice between the two, I would prefer the
original as it's easily comprehensible and to modify.
One of the things I like about Python and Ruby is that they
encourage/enable clean readable code, so I find the above
going against the design philosophy of the language.
Comments? Please, I am not looking for an argument here.
The original question was how to make it more succinct, not more
elegant or transparent. As it happens, I don't think there's anything
particularly unclean or unreadable about the code above, though I've
pretty much given up on "readable" as a meaningful term. All of the
copmlexity in the code comes directly from the original problem, which
requires massaging with sub for some (but not all) possible values,
and indicates the right value indirectly by having the pre-massaged
version be a method name that produces the result 1 when sent to the
video object.... Obviously it would be less cluttered if it could
just be:
expected.concat(Video.find
all).map {|video|
"#{self.id_}#{video.id}#{video.ext}"
})
but that's not indicated by the original code.
Another point is that while Ruby certain does (in my view) provide
favorable conditions for writing nice-looking code, it's important not
to be *too* skittish about the use of constructs that can't always be
picked up at a glance. Some Ruby constructs are easier to read, and
for different people, than others. It was never part of the Ruby
contract, so to speak, that no one would have to make an effort to
learn how to read Ruby.
David
--
* Books:
RAILS ROUTING (new!
http://www.awprofessional.com/title/0321509242)
RUBY FOR RAILS (
http://www.manning.com/black)
* Ruby/Rails training
& consulting: Ruby Power and Light, LLC (
http://www.rubypal.com)