|
|
|
|
|
by parenthephobia
3752 days ago
|
|
@sentence = @sentence.split(' ').map!{|x| x = x[0..0].upcase << x[1..-1]}.join(' ')
Nobody sensible would write this code. The nearest sane version is: @sentence = @sentence.split(' ').map{|x| x[0].upcase + x[1..-1]}.join(' ')
But, although capitalize doesn't do the same thing, it's almost certainly what is actually wanted, so it would be @sentence = @sentence.split(' ').map(&:capitalize).join(' ')
Having said that, it's much more likely that a "clever one-liner" would be @sentence.gsub!(/\b\w/){|x|x.upcase}
Code like the article's would generally not be the result of cleverness, but of code that has organically mutated into something silly you'd never have written from scratch that way. |
|
That said, a block isn't necessary here and the one-liner can be even shorter:
&: call syntax is actually a little faster post 1.9, too.