Recently I adjusted Ruby Facets 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:
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.
Recently I adjusted Ruby Facets 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:
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:
On Tue, May 22, 2012 at 9:51 AM, Intransition <transfire@gmail.com> wrote:
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.
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 Sawyer wrote in post #1061685:
To which end I also added #uppercase and #lowercase methods to alter just the first character. So one can do:
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.
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.
Sorry do no understand: do you want Capitalize or capilatizE?
···
2012/5/22 Eric Christopherson <echristopherson@gmail.com>
On Tue, May 22, 2012 at 9:51 AM, Intransition <transfire@gmail.com> wrote:
> Recently I adjusted Ruby Facets 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.
>
> 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 23/05/2012, at 4:02 AM, Bartosz Dziewoński <matma.rex@gmail.com> 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.
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:
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.
···
On 23 May 2012 02:02, Bartosz Dziewoński <matma.rex@gmail.com> wrote:
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 12:02:21 PM UTC-4, Bartosz Dziewoński 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.
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
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 Tuesday, May 22, 2012 11:23:51 AM UTC-4, Eric Christopherson wrote:
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.)