[#1551] Hashes as keys — "Nathaniel Talbott" <nathaniel@...>

I was just playing around with Hash#hash and discovered that you can't use a

13 messages 2003/09/23

Re: String#downcase! bug/oddness (?)

From: "Gavin Sinclair" <gsinclair@...>
Date: 2003-09-03 01:16:05 UTC
List: ruby-core #1502
> This may or may not be considered a bug, but I don't see it documented
> anywhere, and it's somewhat odd IMO:
>
>     "A".downcase! => "a"
>     "a".downcase! => nil

It's common, and I think controversial, that bang methods typically return
nil when they don't change their receiver.  I would definitely prefer that
they returned the same as their non-in-place counterparts, but there was a
clever example of uniq vs uniq! in ruby-talk recently which gave me food
for thought.

> Now, returns for bang-functions might not seem useful, but consider when
> you have something like:
>
>     get_a_big_string.downcase!
>
> You can presumably gain efficiency by operating on the existing string,
> instead of copying it.  This applies to most of the string functions,
> IMHO, but it's just my opinion after all.

Your presumption could well be wrong.  Once upon a time, I got the idea
that bang methods are usually implemented by generating the desired value
and using #replace to perform the "in-place" aspect.  But probably I'm
wrong.

FWIW, I've become accustomed to thinking of bang methods purely as
statements.  Just like in English, a sentence ending in "!" is commonly a
command.

Gavin



In This Thread

Prev Next