Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

I like that type of code, so yes, maybe it's a darling.

It's easy to make it more readable by aligning the code with the dots, so that it becomes a pipeline:

  @sentence = @sentence.split(' ')
                       .map!{|x| x = x[0..0].upcase << x[1..-1]}
                       .join(' ')
I definitely prefer that to half a screen page of crappy imperative code, where people over time will add lots of side effects etc.

Besides, the middle part is clearly a strawman because

  @sentence = @sentence.split(' ')
                       .map!(&:capitalize)
                       .join(' ')
but yeah. ;)


`map!`, `<<`, and `0..0` are all unnecessary and distracting here. The availability of `capitalize` aside, `map`, `+`, and `0` would be better choices.


As pointed out below, they aren't equivalent.

Compare the output of both when run against "foo bar WIBBLE"




Consider applying for YC's Summer 2026 batch! Applications are open till May 4

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: