Hacker News new | ask | show | jobs
by d883kd8 3752 days ago
Maybe I'm still just showing off, but I don't even use Ruby that often and it's pretty clear what that code does. Split the sentence into words, capitalize the first letter of each word, and re-join the words into a single string.
3 comments

I don't know Ruby and reached the same conclusion in about 15 seconds. Could be nice to make it a function [or name the results] "capitalizeWords(text)". Possibly adding indentation could help:

    @sentence = @sentence.
      split(' ').
      map!{ |x| 
        x = x[0..0].upcase << x[1..-1]
      }.
      join(' ')
[Edit: Fixed code, thanks knodi123]
you want the . at the end of the line, so the interpreter knows to expect a continuation.
I have written my fair share of ruby and yeah, that line didn't take superpowers. The point of the article is still valid, though.
Same. Don't know Ruby at all, but in this case the language is pretty straightforward. Perhaps Ruby was the wrong example. Let's get more magical with Perl:

  map s {(\w+)} {\u$1}g, @sentence;
I'll give a dollar to anyone who already knows Perl and can tell me what the fuck is going on there.
In rough English, it uppercases the first letter every word in every element of the sentence array.