Trying to create a titleize method manually

1. You used .upcase!, and maybe you were just providing an example, but
doesn't that make every letter capital, not just the 1st one? Wouldn't
.capitalize! be more appropriate?

Quite so. I'm trying to teach you the concepts involved, without
handing you too much of a ready-made solution.

2. If I am not mistaken, I believe the square brackets refer to an
array. My intention for the program to react with a varying amount of
words at a time.

Right... but you want the response to be a string. So, you have turn
it back into a string before you're done. I think you've already used
the method you need, in a previous try; now you just need to bring it
back in. It's much more obvious when you use multiple words.

3. I read your blog post, and if I understand the bang(!) correctly it
actually modifies the string of text for the entire method.

Right. It modifies the string of text, whereas "upcase" (with no
bang) returns a modified *copy* of it. In real life analogy, "upcase"
would a person that you show a slip of paper, and he copies down the
contents in uppercase on a new slip and gives that to you. It's up to
you to put that with the rest of your slips (i.e., record it in a
variable), versus just letting it drop to the floor. By contrast,
"upcase!" takes your slip of paper, erases each lowercase letter and
replaces it with its uppercase version, and puts the slip back on the
table with the rest of your slips.

(I would have said that upcase! "hands you back your original slip",
but that's exactly the oversimplification that my blog post was
warning about. It's more like he reads it to you if there were any
changes, else he says nothing, and it's up to you to record the
response (or lack thereof) or ignore it (the usual course of action).)

In this
context, using the ! for capitalize would modify the array

Technically, modify each element (at least, those needing mods), but
yes, I think you've got the basic idea.

so that every
value passed through would be capitalized. This is why you said the
.capitalize comes before the .split, because if it were the other way
around you would only capitalize the 1st word. Correct?

"before" is how you already had it, as x.capitalize.split. You want
to split it first, then capitalize each word (except "little words",
which get different treatment, and even then, capitalize it if it's
the first, but I think we can leave that for later), then put it all
back together.

-Dave

···

trying 2 learn <lists@ruby-forum.com> wrote:

--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/\.

I am aware of poetry style, but that also doesn't titlecase each word in the line. P'raps I should consult my favourite grammarians on this.

···

On Aug 30, 2013, at 11:06 AM, Joel Pearson <lists@ruby-forum.com> wrote:

Technically an explicit newline should be followed by a capital letter,
regardless of the preceding punctuation. This is true in poetry, and as
far as I know, everywhere else. "Word wrap" doesn't count as that's down
to the editor.

Seems to me we went around on this last March. Here's my little ditty from that time: https://github.com/tamouse/titlecase

tamouse m. wrote in post #1120041:

Seems to me we went around on this last March. Here's my little ditty
from that time: GitHub - tamouse/titlecase: Add titlecase methods to String

Very succinct! I like the way you used send :include.
I forgot you could do this: "stopwords=DEFAULT_STOP_WORDS". That would
have made my code a bit neater :slight_smile:

···

--
Posted via http://www.ruby-forum.com/\.

Interesting! I haven't had cause to use the block-form of gsub
before, and here you are, using it twice. I'll have to remember that
trick!

One question though: if it's multiple lines, your gem will uppercase
the first char of every line. Was that your intent? If not, you can
use \A instead of ^.

-Dave

···

On Fri, Aug 30, 2013 at 1:32 AM, Tamara Temple <tamouse.lists@gmail.com> wrote:

Seems to me we went around on this last March. Here's my
little ditty from that time: GitHub - tamouse/titlecase: Add titlecase methods to String

--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/\.

Speaking of using gsub with a block, Bozhidar Batsov JUST wrote a blog
post about it:

  http://batsov.com/articles/2013/08/30/using-gsub-with-a-block/

···

--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/.

Seems to me we went around on this last March. Here's my
little ditty from that time: GitHub - tamouse/titlecase: Add titlecase methods to String

Interesting! I haven't had cause to use the block-form of gsub
before, and here you are, using it twice. I'll have to remember that
trick!

One question though: if it's multiple lines, your gem will uppercase
the first char of every line. Was that your intent? If not, you can
use \A instead of ^.

Actually, I had no thoughts around multiple lines at all. It's an interesting question which way to go with that. Maybe I should offer an option?

···

On Aug 30, 2013, at 8:49 AM, Dave Aronson <rubytalk2dave@davearonson.com> wrote:

On Fri, Aug 30, 2013 at 1:32 AM, Tamara Temple <tamouse.lists@gmail.com> wrote:

-Dave

--
Dave Aronson, the T. Rex of Codosaurus LLC,
secret-cleared freelance software developer
taking contracts in or near NoVa or remote.
See information at http://www.Codosaur.us/\.

Dave Aronson wrote in post #1120093:

Interesting! I haven't had cause to use the block-form of gsub
before, and here you are, using it twice. I'll have to remember that
trick!

I already showed you that! :stuck_out_tongue:

Joel Pearson wrote in post #1119780:

···

def titleize( x )
  x.gsub( /\w+/ ) { |w| w.length > 3 ? w.capitalize : w.downcase
}.capitalize
end

--
Posted via http://www.ruby-forum.com/\.

Well, after consulting with my grammarian pals, I have a new concern. The multi-line thing is not really the issue, per se (except that I should not assume a new line begins a sentence). The issue is what to do after certain punctuation marks:

- sentence ending marks: capitalize next letter
- certain fragment ending marks such as the colon, dash, and so on: capitalize next letter, maybe? (i.e. what follows is considered a subtitle). Example: "Quiet: The Power of Introverts in a World That Can't Stop Talking" — the current implementation would render the "The" following that colon in lower case. Clearly incorrect.

Let's not even get started with I18n issues…

···

On Aug 30, 2013, at 10:49 AM, Tamara Temple <tamouse.lists@gmail.com> wrote:

On Aug 30, 2013, at 8:49 AM, Dave Aronson <rubytalk2dave@davearonson.com> wrote:

One question though: if it's multiple lines, your gem will uppercase
the first char of every line. Was that your intent? If not, you can
use \A instead of ^.

Actually, I had no thoughts around multiple lines at all. It's an interesting question which way to go with that. Maybe I should offer an option?