Is there a better string.each?

a = “canada”
a.each_byte do |i|
puts i.chr
end

thought it was strange myself. personally i’d like it if String and
Array where a little more in harmony. in c/c++ strings are arrays, arn’t
they?

~transami

···

On Thu, 2002-07-04 at 19:52, Tyler Spivey wrote:

well, i have a bit of a problem:
a=“canada”
a.each |i|
do something
end
and, it doesn’t loop over all the characters. is this normal? if so, how do i loop over all the characters?


~transami

“They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety.”
– Benjamin Franklin

a = “canada”
a.each_byte do |i|
puts i.chr
end

thought it was strange myself. personally i’d like it if String and
Array where a little more in harmony.

A String is a string. An Array is an array. Any coincidence that
a String is an array of characters doesn’t mean that a String
converted to an Array should yield an array of characters. You
have String#split for that.

in c/c++ strings are arrays, arn’t they?

Thank goodness Ruby fixed this mistake. :wink:

– Dossy

···

On 2002.07.05, Tom Sawyer transami@transami.net wrote:


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

Tom Sawyer wrote:

thought it was strange myself. personally i’d like it if String and
Array where a little more in harmony. in c/c++ strings are arrays, arn’t
they?

This is something which bites everybody, I think, at some point.

I think things will improve when Ruby gets i17m support. Yes, in Ruby a
String is an array of bytes. What about encodings that support multi-byte
characters? How would you itterate over them? Support for character
itteration /requires/ support for i17m (i18n?), and that is coming. Until
then, there’s String::split and String::each_byte… and even String::.

···

… “If you consider that the average person is stupid, then by
<|> definition, 50% of them are even more dumb.”
/|\ – Dylan Roelofs
/|

Tom Sawyer transami@transami.net writes:

thought it was strange myself. personally i’d like it if String and
Array where a little more in harmony. in c/c++ strings are arrays, arn’t
they?

Perhaps it might be better to say that (currently) the implementation
of Strings is reminiscent of Arrays. However, I’m not sure that this
is a fundamental property of strings, so saying that “strings are
arrays” might be too strong.

Having said that, I’d personally prefer

str.each - returns the characters of str as strings

str.each_byte - returns the characters/bytes of str
(depending on Ruby version)

str.each_line - like the current str#each

But the change to #each would probably break a lot of code…

Dave

no way dude!

if it smells like a fish, and looks like a fish, and tastes like a
fish…

they fact of the matter is: a string is an ordered set of characters and
an an array is an ordered set of objects. thus the array is the general
data structure. a string is a subset of array in that it is the same
type of data structure but limited to characters (which i think is an
object type that is lacking in ruby), yet a string is also a superset of
array in that it has many additional methods.

in effect i could design a string class that inherits array:

class String < Array

and i think that’s how it a ought to be done. but there may very well be
speed considerations, or Regexp considerations, or some such thing, for
why it is not. although i find it odd since ruby is written in c.

ruby did not fix the problem, it complexified matters in this regard.

~transami

···

On Thu, 2002-07-04 at 20:27, Dossy wrote:

On 2002.07.05, Tom Sawyer transami@transami.net wrote:

a = “canada”
a.each_byte do |i|
puts i.chr
end

thought it was strange myself. personally i’d like it if String and
Array where a little more in harmony.

A String is a string. An Array is an array. Any coincidence that
a String is an array of characters doesn’t mean that a String
converted to an Array should yield an array of characters. You
have String#split for that.

in c/c++ strings are arrays, arn’t they?

Thank goodness Ruby fixed this mistake. :wink:

– Dossy


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)


~transami

“They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety.”
– Benjamin Franklin

thought it was strange myself. personally i’d like it if String
and
Array where a little more in harmony. in c/c++ strings are
arrays, arn’t
they?

This is something which bites everybody, I think, at some point.

Not to be a pedant, but this seems like a huge breakage of the POLS,
if it bites everyone, no? =)

···

=====

Use your computer to help find a cure for cancer: http://members.ud.com/projects/cancer/

Yahoo IM: michael_s_campbell


Do You Yahoo!?
Sign up for SBC Yahoo! Dial - First Month Free

I’m definitely all for this. This way, when m17n gets implemented
in Ruby, String#each can return one (or more) bytes, depending on
the appropriate number of bytes for the representation of the
character in the locale’s encoding. String#each_byte literally
returns each byte regardless of locale or encoding, and String#each_line
does what String#each does today.

Is it worth breaking a lot of code to preserve POLS here, though?
Perhaps Ruby 2.x can break backwards compatibility – usually it’s
accepted practice to break backward compatibility in a major
version change.

– Dossy

···

On 2002.07.06, Dave Thomas Dave@PragmaticProgrammer.com wrote:

Tom Sawyer transami@transami.net writes:

thought it was strange myself. personally i’d like it if String and
Array where a little more in harmony. in c/c++ strings are arrays, arn’t
they?

Perhaps it might be better to say that (currently) the implementation
of Strings is reminiscent of Arrays. However, I’m not sure that this
is a fundamental property of strings, so saying that “strings are
arrays” might be too strong.

Having said that, I’d personally prefer

str.each - returns the characters of str as strings

str.each_byte - returns the characters/bytes of str
(depending on Ruby version)

str.each_line - like the current str#each

But the change to #each would probably break a lot of code…


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)

Dossy wrote:

A String is a string. An Array is an array. Any coincidence that
a String is an array of characters

it is by definition, not by coincidence, no?

Tobi

···


http://www.pinkjuice.com/

Dave Thomas wrote:

str.each - returns the characters of str as strings

why not as Characters?

Tobi

···


http://www.pinkjuice.com/

Dave Thomas wrote:

Having said that, I’d personally prefer

str.each - returns the characters of str as strings

str.each_byte - returns the characters/bytes of str
(depending on Ruby version)

str.each_line - like the current str#each

I so completely agree with you, that I’m wondering if you aren’t channeling
my future, dead self.

···

… What is this talk of ‘release’? Klingons do not make software
<|> ‘releases’. Our software ‘escapes’ leaving a bloody trail of
/|\ designers and quality assurance people in its wake.
/|

In many Smalltalks String is either a kind of Array or a sibling class
(subclass of ArrayedCollection, perhaps, as in Squeak).

I do think it’s unfortunate that Ruby has no Character type; equating
characters with integers smells too much like C, and will cause
problems with internationalization.

···

On Thursday 04 July 2002 10:29 pm, Tom Sawyer wrote:

they fact of the matter is: a string is an ordered set of
characters and an an array is an ordered set of objects. thus the
array is the general data structure. a string is a subset of array
in that it is the same type of data structure but limited to
characters (which i think is an object type that is lacking in
ruby), yet a string is also a superset of array in that it has many
additional methods.


Ned Konz
http://bike-nomad.com
GPG key ID: BEEA7EFE

This works fine for German and English Strings. Does this uphold for
other languages too?

···

On Fri, Jul 05, 2002 at 02:29:44PM +0900, Tom Sawyer wrote:

if it smells like a fish, and looks like a fish, and tastes like a
fish…

they fact of the matter is: a string is an ordered set of characters and
an an array is an ordered set of objects. thus the array is the general
data structure. a string is a subset of array in that it is the same
type of data structure but limited to characters (which i think is an
object type that is lacking in ruby), yet a string is also a superset of
array in that it has many additional methods.

in effect i could design a string class that inherits array:

class String < Array

and i think that’s how it a ought to be done.


marko schulz

I see your point, but I disagree.

If Ruby behaved identically to C, then the
surprise for C programmers would be precisely
zero.

But when you use C for string processing (especially
when you’ve used something more powerful), you find
yourself saying: “There should be some easy way to
do THIS…”

In the first place, POLS is a Matz-centric phenomenon
(as he has admitted). The more you think like Matz,
the better you will understand Ruby.

Secondly, POLS should be a meta-linguistic issue IMO –
not “how does this work in my favorite language?” but
“how should this work in an ideal universe?”

And I think Strings are not really Arrays. There are
some isomorphisms there, since they are both “ordered
collections of entities.”

But a string is a highly specialized thing. For one
thing, each item has to be a character. No other
Ruby array is limited in the kind of data it can
contain; such an idea seems very unRubylike to me.

No, I have to say that in this respect as in others,
Ruby corrected C’s mistake. And it was not really
and truly a mistake in C; C is close to assembly
language, and is not as high level as Ruby. There
was not really another way to think of strings
except as arrays. But we’re past that now.

Hal Fulton

···

----- Original Message -----
From: “Michael Campbell” michael_s_campbell@yahoo.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, July 05, 2002 11:07 AM
Subject: Re: is there a better string.each?

This is something which bites everybody, I think, at some point.

Not to be a pedant, but this seems like a huge breakage of the POLS,
if it bites everyone, no? =)

No, not by definition… a string is certainly
not defined as an array (in the computer science
sense) in Ruby.

Strings and arrays just happen to share many
properties in common. So do arrays and hashes
(to a lesser extent).

Hal Fulton

···

----- Original Message -----
From: “Tobias Reif” tobiasreif@pinkjuice.com
To: “ruby-talk ML” ruby-talk@ruby-lang.org
Sent: Friday, July 05, 2002 2:24 PM
Subject: Re: is there a better string.each?

A String is a string. An Array is an array. Any coincidence that
a String is an array of characters

it is by definition, not by coincidence, no?

Michael Campbell wrote:

This is something which bites everybody, I think, at some point.

Not to be a pedant, but this seems like a huge breakage of the POLS,
if it bites everyone, no? =)

Yeah, but this is a known problem that is waiting for the m17n (i18n?)
solution. Man, those are some of the most un-intuitive namings of anything
I’ve ever seen.

···

… “You need someone listening to you for it to be an actual
<|> conversation.”
/|\
/|

I don’t think so; IMO, the PoLS is what is least surprising overall
– not just least surprising for those of us with a C/C++
background. Even though I can wrangle C/C++ ZCHAR strings with the
rest, I am much more comfortable with a native string type.

-austin
– Austin Ziegler, austin@halostatue.ca on 2002.07.05 at 18.03.25

···

On Sat, 6 Jul 2002 01:07:17 +0900, Michael Campbell wrote:

thought it was strange myself. personally i’d like it if String
and Array where a little more in harmony. in c/c++ strings are
arrays, arn’t they?
This is something which bites everybody, I think, at some point.
Not to be a pedant, but this seems like a huge breakage of the
POLS, if it bites everyone, no? =)

I don’t give a lot on the “Principle of least surprise”:

  1. It doesn’t work for me: Ruby surprised me many times and I am happy
    about that.

  2. Many people seem to “abuse” it in discussions: “The feature should
    be that way, because I expect (want) it that way. Else it would
    violate POLS.”

So POLS is a nice thought, but I don’t base too many decisions on it.

···

On Sat, Jul 06, 2002 at 01:07:17AM +0900, Michael Campbell wrote:

Not to be a pedant, but this seems like a huge breakage of the POLS,
if it bites everyone, no? =)


marko schulz

Hard to say, but it is probably worth a poll.

Massimiliano

···

On Sat, Jul 06, 2002 at 02:13:48AM +0900, Dossy wrote:

But the change to #each would probably break a lot of code…
Is it worth breaking a lot of code to preserve POLS here, though?

I may surprise you, or even make mistake (often). But still, since
everyone has different background, I can not satisfy everyone. From
this reason, I don’t count POLS as the rationale for any RCR.

						matz.
···

In message “Re: is there a better string.each?” on 02/07/06, Michael Campbell michael_s_campbell@yahoo.com writes:

Not to be a pedant, but this seems like a huge breakage of the POLS,
if it bites everyone, no? =)

No. Not for Japanese, Chinese, Korean, Russian, (I think) Hebrew …

Well, maybe. I can’t think of a language where a string isn’t a
set of characters in that language, however, when you’re talking
about a computer representation, a string is a sequence of characters
but a “character” in the alphabet can also be a sequence of bytes
itself.

A String might appear at first to be derived from Array, but a String
is really an Array of Characters, where a Character might itself be
an Array of Bytes.

Then, you begin to untangle the holy mess that a single String has
created with regard to the number of Objects that is required to
represent it. IMHO, that’s some serious foul stuff.

– Dossy

···

On 2002.07.06, Marko Schulz in6x059@public.uni-hamburg.de wrote:

On Fri, Jul 05, 2002 at 02:29:44PM +0900, Tom Sawyer wrote:

if it smells like a fish, and looks like a fish, and tastes like a
fish…

they fact of the matter is: a string is an ordered set of characters and
an an array is an ordered set of objects. thus the array is the general
data structure. a string is a subset of array in that it is the same
type of data structure but limited to characters (which i think is an
object type that is lacking in ruby), yet a string is also a superset of
array in that it has many additional methods.

in effect i could design a string class that inherits array:

class String < Array

and i think that’s how it a ought to be done.

This works fine for German and English Strings. Does this uphold for
other languages too?


Dossy Shiobara mail: dossy@panoptic.com
Panoptic Computer Network web: http://www.panoptic.com/
“He realized the fastest way to change is to laugh at your own
folly – then you can let go and quickly move on.” (p. 70)