Opinion on String#camelcase

Recently I adjusted Ruby F.s String#camelcase method to leave the
first
character of the string alone. Previously it would upcase the first
character by default. I never could decide is downcase or upcase should
the
default, so that’s why I ultimately settled on doing neither and leaving
it
to the end-developer to handle. To which end I also added #uppercase and
#lowercase methods to alter just the first character. So one can do:

“foo_man”.camelcase #=> 'fooMan"
“foo_man”.uppercase.camelcase #=> 'FooMan"
“Foo_man”.lowercase.camelcase #=> 'fooMan"

Of course, there is still the optional argument too:

“foo_man”.camelcase(:upper) #=> 'FooMan"
“Foo_man”.camelcase(:lower) #=> 'fooMan"

Does this approach make sense to others?

I know the change probably pissed off a few people, as they had to go
back
and fix some lines of code. But I think the new approach is for the
better.
Yet, just to be sure I want to solicit opinions. Do you agree with this
change, or do you think defaulting to either upper or lower case was
really
the proper approach?

I know I should have done this earlier, but better late then never.

On Tue, May 22, 2012 at 9:51 AM, Intransition [email protected]
wrote:

Recently I adjusted Ruby F.s String#camelcase method to leave the first
character of the string alone. Previously it would upcase the first
character by default. I never could decide is downcase or upcase should the
default, so that’s why I ultimately settled on doing neither and leaving it
to the end-developer to handle. To which end I also added #uppercase and
#lowercase methods to alter just the first character. So one can do:

“foo_man”.camelcase #=> 'fooMan"
“foo_man”.uppercase.camelcase #=> 'FooMan"
“Foo_man”.lowercase.camelcase #=> 'fooMan"

It might be painful, but I’d change the names uppercase and
lowercase to be more specific about just affecting the first
character. With the current names it’s ambiguous whether they convert
all the letters, just the first one, or something else.

(I found myself wondering why you didn’t just use String#capitalize
instead of creating your own uppercase method, but then I found
#capitalize has the surprising [to me] feature of lowercasing all
non-initial letters… so the standard library is not immune to this
kind of naming ambiguity.)

Of course, there is still the optional argument too:

“foo_man”.camelcase(:upper) #=> 'FooMan"
“Foo_man”.camelcase(:lower) #=> 'fooMan"

Does this approach make sense to others?

Sounds good to me.

Thomas S. wrote in post #1061685:

Of course, there is still the optional argument too:

“foo_man”.camelcase(:upper) #=> 'FooMan"
“Foo_man”.camelcase(:lower) #=> 'fooMan"

Does this approach make sense to others?

I would expect a camelcase() method to leave the first character alone
unless I specified an action, so your change is welcome. In addition,
you should provide a default behavior method like
camelcase_defaultfirstchar(arg)
with arg being one of :none, :upper, or :lower with :none as default (or
some kind of well-documented app-wide configuration).

This allows the developer to make their own decision, to change their
mind with one global configuration change, and to upgrade old code with
one configuration change rather than an app-wide search for all uses and
side-effects.

Thomas S. wrote in post #1061685:

To which end I also added #uppercase and
#lowercase methods to alter just the first character. So one can do:

“foo_man”.camelcase #=> 'fooMan"
“foo_man”.uppercase.camelcase #=> 'FooMan"
“Foo_man”.lowercase.camelcase #=> 'fooMan"

The names of the first-char conversion methods should reflect that only
the first character is converted like upcase_first and downcase_first
(The names ‘upcase’ and ‘downcase’ would keep them consistent with the
core String method names).

You probably also want ‘!’ versions of each to perform destructive
conversions on the original String.

I hope you find that helpful.

While your change might be more “correct”, I can’t think of a single
situation in real-world Ruby code where you would need to camelcase
something and keep the first letter small.

Ruby’s constant/module/class names are (by convention) always
UpperCamelCase or UPPERCASE; Ruby’s method and variable names are (by
convention) always lower_snake_case. There’s no reason to convert
anything to lowerCamelCase, unless you’re maybe interfacing with
legacy database or really want to cause yourself trouble by coming up
with different conventions than the usual ones.

Libraries should be optimized for the typical use case. If someone
wants typical camelcasing, they’d just call “.camelcase”; if someone
wants atypical, they’d have to call “.capitalize.camelcase” or
whatever.

– Matma R.

On 23/05/2012, at 4:02 AM, Bartosz Dziewoński [email protected]
wrote:

While your change might be more “correct”, I can’t think of a single
situation in real-world Ruby code where you would need to camelcase
something and keep the first letter small.

Libraries should be optimized for the typical use case. If someone
wants typical camelcasing, they’d just call “.camelcase”; if someone
wants atypical, they’d have to call “.capitalize.camelcase” or
whatever.

I agree with this.

Henry

Sorry do no understand: do you want Capitalize or capilatizE?

2012/5/22 Eric C. [email protected]

instead of creating your own uppercase method, but then I found

Does this approach make sense to others?
really
the proper approach?

I know I should have done this earlier, but better late then never.


Analista de Sistemas
*MBA em
Logística:http://www.logisticadescomplicada.com/a-auto-regulacao-dos-servicos-de-transporte-publico-urbano-de-passageiros/Gerência
Executiva de Transportes, Mobilização e Meio Ambiente - GETRAM
*
*
*
Provedor http://www.InstitutoFederalista.com/debate/ de Serviços na
InterNet

Atenção: Esta carta pode conter anexos no formato ODF (Open Document
Format
)/ABNT (extensões odt, ods, odp, odb, odg). Antes de
pedir os anexos em outro formato, você pode instalar gratuita e
livremente
o LibreOffice http://www.libreoffice.org/download ou o seguinte
Aditivo
para Microsoft Office (R) (
Hardware | Oracle),ou
aquihttp://katana.oooninja.com/w/odf-converter-integrator
.
Para compatibilidade com as normas ISO sugere-se o uso do
LibreOfficehttp://www.libreoffice.org/
.

Para ficar livre dos vírus user www.CentOS.org ou www.FedoraProject.org.

Converse na rede em:

Escolha um apelido para você - nick name - e o canal
#Federalistas(Channel).
A senha é “federalistas”.

*CONSOLIDAÇÃO DAS LEIS DAS PROFISSÕES
LIBERAIShttp://www.causes.com/causes/611364
*
*
*

On Tuesday, May 22, 2012 12:02:21 PM UTC-4, Bartosz Dziewoński wrote:

with different conventions than the usual ones.

Libraries should be optimized for the typical use case. If someone
wants typical camelcasing, they’d just call “.camelcase”; if someone
wants atypical, they’d have to call “.capitalize.camelcase” or
whatever.

You make a good point Matma. And you caused to me to recollect one of the
prior changes that made this change even possible in the first place. A
while back I separated out the need for converting paths, methods and
module/class names to dedicated methods, rather than trying to double
duty
the #camelcase and #snakecase methods for this purposes. So we’re okay
in
this regard. In the particular case you mention we have String#modulize.

On Tuesday, May 22, 2012 11:23:51 AM UTC-4, Eric C. wrote:

non-initial letters… so the standard library is not immune to this
kind of naming ambiguity.)

Yep. that’s exactly why I ended up used #uppercase instead. With
#capitalize off limits I couldn’t think of any better method names at
the
time. But I agree with you in principle and would be open to any good
suggestions.

For the record, I asked Matz to change the behavior of #capitalize some
time ago, but “his idea of capitalize” (as he put it) was the current
one.
Personally I think that’s too bad because it’s easy enough to do
.downcase.capitalize if that’s what one wants. Typical I don’t think
it
is. Couldn’t hurt to lets matz know your thoughts on this too.

Thanks.

On 23 May 2012 02:02, Bartosz Dziewoński [email protected] wrote:

While your change might be more “correct”, I can’t think of a single
situation in real-world Ruby code where you would need to camelcase
something and keep the first letter small.

Ruby’s constant/module/class names are (by convention) always
UpperCamelCase or UPPERCASE; Ruby’s method and variable names are (by
convention) always lower_snake_case. There’s no reason to convert
anything to lowerCamelCase, unless you’re maybe interfacing with
legacy database or really want to cause yourself trouble by coming up
with different conventions than the usual ones.

Not everyone who writes Ruby scripts uses them to generate other Ruby
scripts.

Libraries should be optimized for the typical use case. If someone
wants typical camelcasing, they’d just call “.camelcase”; if someone
wants atypical, they’d have to call “.capitalize.camelcase” or
whatever.

I agree in principle, but I’d suggest that the default behaviour
explicitly leave the first character as-is, with options to force
upper- or lower-casedness*. By your assertion (that lowerCamelCase is
typical and should be supported by default) I would assume the
following would be the same:

“foo_bar”.camelcase #=> “fooBar”
“foo_bar”.capitalize.camelcase #=> “fooBar”
“FOO”_BAR.camelcase #=> “fooBar”

Which suggested that .capitalize should have an optional :none
parameter to force it to leave the character as is, if that’s what you
want. Either way, as long as it’s clearly documented.

  • so back to the original post: yes, I think you’ve done the right
    thing.


Matthew K., B.Sc (CompSci) (Hons)
http://matthew.kerwin.net.au/
ABN: 59-013-727-651

“You’ll never find a programming language that frees
you from the burden of clarifying your ideas.” - xkcd