Ruby-dev summary 26385-26467

From: SASADA Koichi [mailto:ko1@atdot.net]
Sent: Monday, July 18, 2005 8:40 AM
To: ruby-talk ML
Subject: ruby-dev summary 26385-26467

<snip>

NARUSE, Yui proposed that Ruby HEAD should include following
functions.

1) String#each_byte (without block) #=> Array
    It's same function as "'string'.unpack('C*')".
2) String#each_char (without block) #=> Array
3) String#each_char (with block) #=> str
    String#each_char is same as "'string'.split(//).each".

Matz rejected (1) and (2), because it's not feature for
"each". He agreed to import these method if there are any
suitable name.

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'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.

Regards,

Dan

···

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

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.

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