From: Josh Cheek Date: 2013-02-20T04:32:07+09:00 Subject: Re: Adding camelize and underscore to String --20cf300fb03520df5e04d618927b Content-Type: text/plain; charset=ISO-8859-1 On Tue, Feb 19, 2013 at 10:57 AM, Robert Klemme wrote: > On Tue, Feb 19, 2013 at 4:45 AM, Josh Cheek wrote: > > On Mon, Feb 18, 2013 at 9:23 PM, tamouse mailing lists > > wrote: > >> > >> I do this a bit differently: > >> > >> class String > >> def camelize > >> self = self.dup > >> self.camelize! > >> end > > > > Should just be: > > def camelize > > dup.camelize! > > end > > I'd do > > def camelize > dup.tap {|s| s.camelize!} > end > > or even > > def camelize > dup.tap &:camelize! > end > > Because usually #camelize! would return nil if the String was left > unchanged. In any case, this construct would make #camelize robust > against changes in #camelize!'s return value. > > I guess I wouldn't ever implement camelize! to return nil. I checked ActiveSupport, and it is apparently not a method. -Josh --20cf300fb03520df5e04d618927b Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable On Tue, Feb 19, 2013 at 10:57 AM, Robert Klemme <shortcutter@goog= lemail.com> wrote:
On Tue, Feb 19, 2013 at 4:45 AM, Josh Cheek <josh.cheek@gmail.com> wrote:
> On Mon, Feb 18, 2013 at 9:23 PM, tamouse mailing lists
> <tamouse.lists@gmail.com= > wrote:
>>
>> I do this a bit differently:
>>
>> class String
>> =A0 def camelize
>> =A0 =A0 self =3D self.dup
>> =A0 =A0 self.camelize!
>> =A0 end
>
> Should just be:
> def camelize
> =A0 dup.camelize!
> end

I'd do

def camelize
=A0 dup.tap {|s| s.camelize!}
end

or even

def camelize
=A0 dup.tap &:camelize!
end

Because usually #camelize! would return nil if the String was left
unchanged. =A0In any case, this construct would make #camelize robust
against changes in #camelize!'s return value.


I guess I wouldn't ever impl= ement camelize! to return nil. I checked ActiveSupport, and it is apparentl= y not a method.

-Josh
--20cf300fb03520df5e04d618927b--