3 ms·
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
by d883kd8 11y 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.
- dangerlibrary 11y agoI have written my fair share of ruby and yeah, that line didn't take superpowers. The point of the article is still valid, though.
- pacala 11y agoI 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]
- knodi123 11y agoyou want the . at the end of the line, so the interpreter knows to expect a continuation.
- peterwwillis 11y agoSame. 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.
- chromatic 11y agoIn rough English, it uppercases the first letter every word in every element of the sentence array.