From: "Michael W. Ryder" <_mwryder@...> Date: 2007-04-24T07:30:07+09:00 Subject: Re: Question regarding design of the String Class Robert Dober wrote: > On 4/23/07, Michael W. Ryder <_mwryder@worldnet.att.net> wrote: >> Brian Candler wrote: >> > On Tue, Apr 24, 2007 at 02:40:09AM +0900, Michael W. Ryder wrote: >> >> I guess my point was that str[i] behaves totally different from all >> the >> >> other implementations of []. All of the others return a string. >> > >> > You clearly know a lot of languages then :-) >> > >> >> I probably should have phrased that differently. What I meant that all >> of the other implementations of [] in Ruby for the String class return a >> string, only str[i] returns a number. > I copy that, you have made a somehow valid point, that has been > discussed before and do not like either that "ab"[0] == ?a (instead > of "a"). But it is not a clearcut error either. > > The overloading (in human terms not computer science terms) of [] to > get elements and substrings of a string might not be the best choice > either. And that there is String#each_byte and not > String#each_character might hurt too. > But there are other tools around that make up for it. >> >> > As pointed out before, in C, str[i] is an expression whose value is an >> > integer for the character at position i, exactly as in Ruby. >> > >> > In Perl, it doesn't do what you expect either: >> > >> > $ perl -e '$a = "abcde"; print $a[2], "\n";' >> > >> > $ >> > >> > (what this actually does is extract an element from the array @a, >> which I >> > have not initialised, and is completely unrelated to the scalar $a) >> > >> >> In Business Basic or C if I want the numeric value of a character in a >> >> string I specify that. Likewise if I want to copy a string from an >> >> arbitrary position I don't have to specify an ending character like >> >> Ruby. I just find s = t[i, -1] to be much harder to understand in a >> >> quick read then s = t[i]. Others may not have this problem. >> > > I guess that the influence of *Basic and C* to Ruby are minimal. In > order to convince a rubyist that other features might be nice because > they are present in language X, I'd rather chose X from Python, Lisp, > Smalltalk, Self, IO or Lua (and I am leaving out some by laziness and > ignorance) I am not worried about the influence of other languages or trying to make Ruby like language X, I am trying to understand the logic behind some of the choices. It makes it hard when at first glance it looks familiar, but then you get bitten because of the differences that are not readily apparent. >> > Personally I would be *very* surprised if str[i] returned all the >> characters >> > from 'i' to the end of the string. But then I don't program in Business >> > Basic. >> > >> Business Basic has been doing this for over the 25 years I have been >> programming in it. For example if I enter: A$="abcdefg" and then say: >> Print A$(3) it prints cdefg. Like Ruby, if I enter B$=A$(3,3) B$ >> contains cde. Other than the beginning number of the string they act >> the same. >> >> > I do program in C though. If I wanted the string from position i to >> the end >> > of the string, I would write str + i, or possibly &str[i] >> > >> >> But you do not have to provide a length or ending position for the copy >> which was part of my confusion. I specify a starting position and the >> language copies the rest of the string. In Ruby just providing a >> starting position gives me a numeric value. > Well if you want to get the maximum from Ruby I'd advice you, sorry if > this is sounding blunt, to take a break from too much comparing with > other languages. > Paradigm shifts are tough, after that break you might still think that > "ab"[0] == ?a is > not a good thing, but I am sure that you will be able to bring your > point across much better. > > Sorry if I became lecturing, just thought it might help, after all ;). > I remember very well when I was lectured about duck typing, first I > was angry, and I said lots of stupid things (they were very clever in > my Ada world of course), but when I let go and looked at things as > they were I really shifted into the paradigm of Ruby, and yes I still > get bitten by duck typing and no I do not introduce type checking, I > just write better tests. > I program for a living in Business Basic so I can't take a break from the language to learn Ruby. Instead, I am trying to convert functions from Business Basic to Ruby and may consider then trying to convert some of my programs to Ruby. I have already completed one of the function conversions and may post the results later. Part of the reason for the conversion would be the ability to "upgrade" the programs to use things like browsers and SQL instead of the glass tty and flat files built into Business Basic. > Welcome to Ruby. > > Cheers > Robert > >