Ruby-dev summary 26385-26467

From: Ryan Leavengood [mailto:mrcode@netrox.net]
Sent: Monday, July 18, 2005 9:58 AM
To: ruby-talk ML
Subject: Re: ruby-dev summary 26385-26467

Berger, Daniel said:
>
> What about simply altering String#to_a?
>
> String#to_a(bytes=false)
> Returns the string as an array of characters, or bytes if the
> +bytes+ argument is true.

I *strongly* dislike parameterizing methods like this. If you
are going to fundamentally alter the behavior of a method,
just make a new method. That way the new behavior is explicit.

If I read this code:

"str".to_a(true)

I will probably have to read the documentation for to_a to
figure out what the true means. Whereas this:

"str".to_bytes # Or whatever it is called

is essentially self-documenting.

Generally speaking I agree with you, but I also try to weigh that
consideration against API bloat. And, since the default (false) is
probably what most people would use most of the time, it's a bullet I'd
be willing to bite. However, all of this probably academic, as you
mention below.

> I've never really liked the current implementation, where
"foo".to_a
> merely becomes ["foo"] - it just isn't useful usually.
However, I'm
> also aware that modifying String#to_a could cause major
breakage for
> many current packages (mine included). Is it worth it?
I'll let Matz
> decide.

The usefulness of the current String#to_a is certainly
debateable, but changing it is definitely too big of a change
to be viable.

Ryan

Yes, probably. Plus, Matz tends to dissuade folks from thinking of
strings as an array of characters, even though we're constantly breaking
down strings into arrays of characters. :wink:

Well, there's always Sydney. :slight_smile:

Regards,

Dan

···

-----Original Message-----

you can always have the best of both worlds with something like

   "string".to_a 'bytes' => true

the beauty of this sort of api is that later you can add

   "string".to_a 'bytes' => true, 'unicode' => true

and break nothing.

cheers.

-a

···

On Tue, 19 Jul 2005, Berger, Daniel wrote:

-----Original Message-----
From: Ryan Leavengood [mailto:mrcode@netrox.net]
Sent: Monday, July 18, 2005 9:58 AM
To: ruby-talk ML
Subject: Re: ruby-dev summary 26385-26467

Berger, Daniel said:

What about simply altering String#to_a?

String#to_a(bytes=false)
   Returns the string as an array of characters, or bytes if the
+bytes+ argument is true.

I *strongly* dislike parameterizing methods like this. If you
are going to fundamentally alter the behavior of a method,
just make a new method. That way the new behavior is explicit.

If I read this code:

"str".to_a(true)

I will probably have to read the documentation for to_a to
figure out what the true means. Whereas this:

"str".to_bytes # Or whatever it is called

is essentially self-documenting.

Generally speaking I agree with you, but I also try to weigh that
consideration against API bloat. And, since the default (false) is
probably what most people would use most of the time, it's a bullet I'd
be willing to bite. However, all of this probably academic, as you
mention below.

--

email :: ara [dot] t [dot] howard [at] noaa [dot] gov
phone :: 303.497.6469
My religion is very simple. My religion is kindness.
--Tenzin Gyatso

===============================================================================