@martinrinehart said:
The more Ruby I write, the more readable my Ruby becomes. Odd, but the secret seems to be to do nothing the way the Rubyists In Crowd does things. Here's a useful function:
> def concat( *args )
> ret = ''
> for a in args do ret += ( a.to_s() ) end
> return ret
> end
>
Here's the official, Rubyists-approved-but-don't-show-Monty version of that function:
> def concat(*args)
> ret = ''
> args.each {|a| ret += a.to_s}
> ret
> end
>
personally, I find the second version much more readable, especially when just scanning the code. I takes me about 5 steps to glance at the second version, while it takes about 8-10 steps for the first.
listing the code in the order I see it:
version 1:
( a.to_s() ) end
(line 3)
concat( *args )
(line 1)
for a in args
(line 3)
ret = ''
(line 2)
return ret
(line 4)
do ret +=
(line 3)
(it usually takes 2 - 3 more steps to put everything together)
version 2:
{|a| ret += a.to_s}
(line 3)
concat(*args)
(line 1) ( I can usually understand the code by this step)
ret = ''
(line 2)
args.each
(line 3)
ret end
(line 4-5)
Everything I don't have listed, I skip.
BTW, my version would be:
def concat(*args)
return args.join('')
end