a titleize function?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|

a titleize function?

sergio_101-2
is there a function that can do this:

'a mouse ate cheese' . 'A Mouse Ate Cheese'

i found capitlize, but that only hits the first word..

thanks!


--

----
peace,
sergio
photographer, journalist, visionary

http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: a titleize function?

Enrico Schwass-2
sergio_101 <[hidden email]> writes:

Hello

> is there a function that can do this:
>
> 'a mouse ate cheese' . 'A Mouse Ate Cheese'
>
> i found capitlize, but that only hits the first word..

There is a shorter way, I guess, but

('a mouse ate cheese' substrings inject: '' into: [:start :each | start,
' ', each capitalized])

does it for me.

bye
Enno
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: a titleize function?

Enrico Schwass-2
In reply to this post by sergio_101-2
sergio_101 <[hidden email]> writes:

> is there a function that can do this:
>
> 'a mouse ate cheese' . 'A Mouse Ate Cheese'
>
> i found capitlize, but that only hits the first word..
>
> thanks!

In addition to my previous post and to get rid of the first space, just
use the allButFirst message.

Anyway, I am sure more experienced Smalltalkers can present a cleaner way.
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: a titleize function?

sergio_101-2
i know there is one in rails.. i might just look at the code and adapt it..

but.. this makes me wonder..

since this would probably be a handy function to have in strings...

should i add it to a personal library/collection, or add it directly
to strings? or extend strings?

how do smalltalkers usually do this kind thing?

--

----
peace,
sergio
photographer, journalist, visionary

http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101
_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: a titleize function?

David Mitchell-10
In Smalltalk, as in Ruby, just add the useful method to the class. (You could also add the code to an instance, but it is far less common in Smalltalk than in Ruby.)

You can still put this in a "library." The de facto library in Squeak is a Monticello package.

You 'create' a Monticello package by creating a class category in the browser. (For example, MyApplication-Core or MyApplication-View or MyApplication-Tests).

Monticello can keep your extensions to other people's classes by adding a specially named method category. It has to start with an asterisk (*) and then match the first part of your class category. For example, *myapplication or *myapplication-printing

When you share your Monticello .mcz, it will automatically include your classes and any extensions to other classes. I'm using the term extension in a Smalltalky way, as opposed to using the term monkey-patching.

If you are writing an application, usually you will have no problems.

If you are writing libraries that others will find useful, then you may want to "namespace" your methods by adding a prefix indicating your application. The Magma OO database defines a method magmaOid on Object rather than taking the name oid.


On Fri, Mar 19, 2010 at 11:21 AM, sergio_101 <[hidden email]> wrote:
i know there is one in rails.. i might just look at the code and adapt it..

but.. this makes me wonder..

since this would probably be a handy function to have in strings...

should i add it to a personal library/collection, or add it directly
to strings? or extend strings?

how do smalltalkers usually do this kind thing?

--

----
peace,
sergio
photographer, journalist, visionary

http://www.coffee-black.com
http://www.painlessfrugality.com
http://www.twitter.com/sergio_101
http://www.facebook.com/sergio101
_______________________________________________


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: a titleize function?

David T. Lewis
In reply to this post by sergio_101-2
On Fri, Mar 19, 2010 at 02:31:40AM -0400, sergio_101 wrote:
> is there a function that can do this:
>
> 'a mouse ate cheese' . 'A Mouse Ate Cheese'
>
> i found capitlize, but that only hits the first word..

Here is another way to do it:

String>>titleize
    "Answer a copy of myself with the first character of each word capitalized "
    " 'a mouse ate cheese' titleize "
    ^ String streamContents: [:strm | self
            inject: Character space
            into: [:last :this | strm
                    nextPut: (last = Character space
                                ifTrue: [this asUppercase]
                                ifFalse: [this])]]

Explanation:

The #inject:into: steps through the string, keeping track of the
preceding character and writing the current character to a stream,
converting it to upper case if the preceding character was a space.

The result of the #nextPut: is the character we wrote, which appears
in the #last variable each time we step through the loop.

The #streamContents: method provides the stream that we write the
characters to, and answers the resulting string when complete.

A change set is attached.

Dave


_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

String-titleize-dtl.1.cs (5K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: a titleize function?

David T. Lewis
I should note also that class Character has methods for testing
different kinds of characters (#isDigit and so forth). So if you
wanted your method to work for strings that might contain tab
characters or carriage returns as separator characters between
words, then you would use the #isSeparator test rather than looking
for space characters explicitly, like this:

String>>titleize
    "Answer a copy of myself with the first character of each word capitalized "
    " 'a mouse ate cheese' titleize "
    ^ String streamContents: [:strm | self
            inject: Character space
            into: [:last :this | strm
                    nextPut: (last isSeparator
                                ifTrue: [this asUppercase]
                                ifFalse: [this])]]


On Sat, Mar 20, 2010 at 11:05:02AM -0400, David T. Lewis wrote:

> On Fri, Mar 19, 2010 at 02:31:40AM -0400, sergio_101 wrote:
> > is there a function that can do this:
> >
> > 'a mouse ate cheese' . 'A Mouse Ate Cheese'
> >
> > i found capitlize, but that only hits the first word..
>
> Here is another way to do it:
>
> String>>titleize
>     "Answer a copy of myself with the first character of each word capitalized "
>     " 'a mouse ate cheese' titleize "
>     ^ String streamContents: [:strm | self
>             inject: Character space
>             into: [:last :this | strm
>                     nextPut: (last = Character space
>                                 ifTrue: [this asUppercase]
>                                 ifFalse: [this])]]
>
> Explanation:
>
> The #inject:into: steps through the string, keeping track of the
> preceding character and writing the current character to a stream,
> converting it to upper case if the preceding character was a space.
>
> The result of the #nextPut: is the character we wrote, which appears
> in the #last variable each time we step through the loop.
>
> The #streamContents: method provides the stream that we write the
> characters to, and answers the resulting string when complete.
>

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: a titleize function?

John McKeon
A correct "titlization" will not capitalize "small words" (articles, prepositions etc). This is probably overkill but I've got parser madness and just could not help myself =)

The attached mcz package will require Lukas Renggli's PetitParser from the repository at http://source.lukas-renggli.ch/petit

Once loaded you can evaluate 'of mice and men' asTitle to get 'Of Mice and Men' 

John


On Sat, Mar 20, 2010 at 11:18 AM, David T. Lewis <[hidden email]> wrote:
I should note also that class Character has methods for testing
different kinds of characters (#isDigit and so forth). So if you
wanted your method to work for strings that might contain tab
characters or carriage returns as separator characters between
words, then you would use the #isSeparator test rather than looking
for space characters explicitly, like this:

String>>titleize
   "Answer a copy of myself with the first character of each word capitalized "
   " 'a mouse ate cheese' titleize "
   ^ String streamContents: [:strm | self
           inject: Character space
           into: [:last :this | strm
                   nextPut: (last isSeparator
                               ifTrue: [this asUppercase]
                               ifFalse: [this])]]


On Sat, Mar 20, 2010 at 11:05:02AM -0400, David T. Lewis wrote:
> On Fri, Mar 19, 2010 at 02:31:40AM -0400, sergio_101 wrote:
> > is there a function that can do this:
> >
> > 'a mouse ate cheese' . 'A Mouse Ate Cheese'
> >
> > i found capitlize, but that only hits the first word..
>
> Here is another way to do it:
>
> String>>titleize
>     "Answer a copy of myself with the first character of each word capitalized "
>     " 'a mouse ate cheese' titleize "
>     ^ String streamContents: [:strm | self
>             inject: Character space
>             into: [:last :this | strm
>                     nextPut: (last = Character space
>                                 ifTrue: [this asUppercase]
>                                 ifFalse: [this])]]
>
> Explanation:
>
> The #inject:into: steps through the string, keeping track of the
> preceding character and writing the current character to a stream,
> converting it to upper case if the preceding character was a space.
>
> The result of the #nextPut: is the character we wrote, which appears
> in the #last variable each time we step through the loop.
>
> The #streamContents: method provides the stream that we write the
> characters to, and answers the resulting string when complete.
>

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners



--
http://jmck.seasidehosting.st

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners

Parsers-jmck.1.mcz (2K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: a titleize function?

Bert Freudenberg
On 20.03.2010, at 21:05, John McKeon wrote:
> A correct "titlization" will not capitalize "small words" (articles, prepositions etc). This is probably overkill but I've got parser madness and just could not help myself =)

Seems like overkill for the beginners list indeed ;)

Besides, the only general-purpose "titlization" method is "Start case", which capitalizes each word without exceptions.

It's purely a matter of style which words to capitalize in a title:

http://en.wikipedia.org/wiki/Letter_case#Headings_and_publication_titles

If you want to discuss this further for possible inclusion in Squeak, please move the discussion to squeak-dev.

- Bert -

_______________________________________________
Beginners mailing list
[hidden email]
http://lists.squeakfoundation.org/mailman/listinfo/beginners