[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

The Pebble in the Ruby Shoe

Raphael Gillett

7/16/2007 1:22:00 PM



Ruby aims to be a human friendly programming language that embodies the
principle of least surprise. However, there is an important feature of
the language that, despite being a glaring exception to this laudable
goal, seems to have crept unquestioned into the Ruby core. It is a
rebarbative and truly medieval practice which everyone knows causes
endless confusion, countless unnecessary errors, and a great deal of
wasted programming time in all languages that incorporate it, e.g., C,
Java. In violation of Ruby's ethos, this feature is present purely to
suit the compiler/interpreter at the expense of the person.

The pebble in the Ruby shoe is the counter-intuitive use of indexing of
elements of arrays, strings, etc., from 0 to n-1 instead of the more
natural 1 to n. Like prisoners who have got used to their chains, the
Ruby community seems to have meekly accepted this impediment to clear
thinking. It is particularly puzzling that Ruby should hobble its
users in this way, because it is manifestly unnecessary (e.g., Fortran
and Pascal don't do it).

--
Posted via http://www.ruby-....

93 Answers

SonOfLilit

7/16/2007 1:31:00 PM

0

I disagree. Zero based arrays are natural to a mathematician, and
programmers tend to be mathematicians.

It's humanity that has the wrong clue. I hate the problems that
one-based counting creates. Anywhere, not just in arrays.

Aur

Lionel Bouton

7/16/2007 1:32:00 PM

0

Raphael Gillett wrote the following on 16.07.2007 15:22 :
> [...]
> The pebble in the Ruby shoe is the counter-intuitive use of indexing of
> elements of arrays, strings, etc., from 0 to n-1 instead of the more
> natural 1 to n. Like prisoners who have got used to their chains, the
> Ruby community seems to have meekly accepted this impediment to clear
> thinking. It is particularly puzzling that Ruby should hobble its
> users in this way, because it is manifestly unnecessary (e.g., Fortran
> and Pascal don't do it).
>
>

It's probably a matter of taste: I'm so used to indices starting at 0
that I would probably create a lot of bugs if I had to use arrays with
indices starting at 1.

For me an index has simply become a distance from the first element of
an array.

Anyway changing this would probably break at least 99% of all Ruby code
out there, so it's not likely to happen, is it?

Lionel.

Sammy Larbi

7/16/2007 1:47:00 PM

0

Raphael Gillett wrote, On 7/16/2007 8:22 AM:
> Ruby aims to be a human friendly programming language that embodies the
> principle of least surprise. However, there is an important feature of
> the language that, despite being a glaring exception to this laudable
> goal, seems to have crept unquestioned into the Ruby core. It is a
> rebarbative and truly medieval practice which everyone knows causes
> endless confusion, countless unnecessary errors, and a great deal of
> wasted programming time in all languages that incorporate it, e.g., C,
> Java. In violation of Ruby's ethos, this feature is present purely to
> suit the compiler/interpreter at the expense of the person.
>
> The pebble in the Ruby shoe is the counter-intuitive use of indexing of
> elements of arrays, strings, etc., from 0 to n-1 instead of the more
> natural 1 to n. Like prisoners who have got used to their chains, the
> Ruby community seems to have meekly accepted this impediment to clear
> thinking. It is particularly puzzling that Ruby should hobble its
> users in this way, because it is manifestly unnecessary (e.g., Fortran
> and Pascal don't do it).
>
>

Well now would be a hell of a time to change it, wouldn't it? =)


Michael Ulm

7/16/2007 1:58:00 PM

0

Raphael Gillett wrote:
>
>
> The pebble in the Ruby shoe is the counter-intuitive use of indexing of
> elements of arrays, strings, etc., from 0 to n-1 instead of the more
> natural 1 to n. Like prisoners who have got used to their chains, the
> Ruby community seems to have meekly accepted this impediment to clear
> thinking. It is particularly puzzling that Ruby should hobble its
> users in this way, because it is manifestly unnecessary (e.g., Fortran
> and Pascal don't do it).
>

I, for one absolutely hate 1 based arrays in programming languages.
It creates a thousand little problems that one has to program around.
Ruby got it Right (again).

Lloyd Linklater

7/16/2007 2:55:00 PM

0

Raphael Gillett wrote:
> The pebble in the Ruby shoe is the counter-intuitive use of indexing of
> elements of arrays, strings, etc., from 0 to n-1 instead of the more
> natural 1 to n. Like prisoners who have got used to their chains, the
> Ruby community seems to have meekly accepted this impediment to clear
> thinking. It is particularly puzzling that Ruby should hobble its
> users in this way, because it is manifestly unnecessary (e.g., Fortran
> and Pascal don't do it).

Well, why don't you make a new class that uses a hash as an array and
set the range to be from 1..X? A really cool thing about Ruby is that
making such things is simple and encouraged.

--
Posted via http://www.ruby-....

Axel Etzold

7/16/2007 3:06:00 PM

0

> Raphael Gillett wrote the following on 16.07.2007 15:22 :
> > [...]
> > The pebble in the Ruby shoe is the counter-intuitive use of indexing of
> > elements of arrays, strings, etc., from 0 to n-1 instead of the more
> > natural 1 to n. Like prisoners who have got used to their chains, the
> > Ruby community seems to have meekly accepted this impediment to clear
> > thinking.

Dear Raphael,

from http://www.americanscientist.org/template/AssetDetail/assetid/51982?&...,

an overview article about the history of around 6000 computer programming
languages:



"Still another perennially contentious issue is how to count. This one brings out the snarling dogmatism in the meekest programmer. Suppose we have a list of three items. Do we number them 1, 2, 3, or should it be 0, 1, 2? Everyone in computerdom knows the answer to that question, and knows it as an eternal truth held with the deepest, visceral conviction. Only one of the alternatives is logically tenable. But which is it ?.."

If you strongly believe that Ruby gets it all wrong, you can do all
the iterations with

Array.each{|item| ... } or
Array.each_with_index{|item,i| ... } # <= don't need to know where
you start counting actually

> > It is particularly puzzling that Ruby should hobble its
> > users in this way, because it is manifestly unnecessary (e.g., Fortran
> > and Pascal don't do it).

If you check out different wordings of Peano's axioms, which define
the natural numbers, you'll find that they'll work regardless of whether
you name the first element 0 or 1 or 175758, so it's not necessary
to start at 0 or at 1 or anywhere else, but in the implementation
of a programming language, you sometimes have to make a convention choice.

Ruby, in contrast to some languages I used to work with before
(e.g., Matlab, which is a one-language, or C, which is a zero-language), lets you do your work even if you don't adhere to the convention choice made thanks to the each_with_index method.

Also, sometimes, you could prefer to count still differently:
e.g., January, February ,....


year.each{|month| p "Have a nice " + month }

I find that this can increase readability of a program a great deal...


Best regards,

Axel
--
Psssst! Schon vom neuen GMX MultiMessenger gehört?
Der kanns mit allen: http://www.gmx.net/de/go/mult...

John Joyce

7/16/2007 3:22:00 PM

0


On Jul 16, 2007, at 8:58 AM, Michael Ulm wrote:

> Raphael Gillett wrote:
>> The pebble in the Ruby shoe is the counter-intuitive use of
>> indexing of
>> elements of arrays, strings, etc., from 0 to n-1 instead of the more
>> natural 1 to n. Like prisoners who have got used to their
>> chains, the
>> Ruby community seems to have meekly accepted this impediment to clear
>> thinking. It is particularly puzzling that Ruby should hobble its
>> users in this way, because it is manifestly unnecessary (e.g.,
>> Fortran
>> and Pascal don't do it).
>
> I, for one absolutely hate 1 based arrays in programming languages.
> It creates a thousand little problems that one has to program around.
> Ruby got it Right (again).
>

Every language does things the way the language designer wants to do it.
see this article for some discussion on it:
http://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/E...

AppleScript also uses 1 based arrays.
The practicality of any of several approaches is largely subjective.
Ideally there would be a flag to set the type of indexing you want to
use.
In Ruby, you could certainly do something to implement it, but it
would cast some overhead unless you created something like class
Array1 to be analogous to class Arrray, just indexed from 1 instead.
Feel free to create it and see if it would get added in.
It might certainly be useful as an option for some people, but in
general it is pretty simple to adjust between the paradigms.
Work the way a language works, don't try to make it work the way
another language works.

Many people, myself included, were first confounded by this in C, but
you adapt quickly.
0 indexing is convenient in that the element equal to the length is
out of bounds or contains a special character/marker to denote the
end of the array, depending on the language.

Some things are just continued because of convention. Most languages
use 0-based indexing.

Bertram Scharpf

7/16/2007 3:41:00 PM

0

Hi,

Am Montag, 16. Jul 2007, 22:22:05 +0900 schrieb Raphael Gillett:
> The pebble in the Ruby shoe is the counter-intuitive use of indexing of
> elements of arrays, strings, etc., from 0 to n-1 instead of the more
> natural 1 to n.

To start indexing of arrays from 0 is any good programmers
-first- sorry zeroth lesson. It is the natural way. Get used
to it.

Bertram


--
Bertram Scharpf
Stuttgart, Deutschland/Germany
http://www.bertram-...

Robert Dober

7/16/2007 3:51:00 PM

0

On 7/16/07, Axel Etzold <AEtzold@gmx.de> wrote:
> > Raphael Gillett wrote the following on 16.07.2007 15:22 :
> > > [...]
> > > The pebble in the Ruby shoe is the counter-intuitive use of indexing of
> > > elements of arrays, strings, etc., from 0 to n-1 instead of the more
> > > natural 1 to n. Like prisoners who have got used to their chains, the
> > > Ruby community seems to have meekly accepted this impediment to clear
> > > thinking.
>
> Dear Raphael,
>
> from http://www.americanscientist.org/template/AssetDetail/assetid/51982?&...,
>
> an overview article about the history of around 6000 computer programming
> languages:
>
>
>
> "Still another perennially contentious issue is how to count. This one brings out >the snarling dogmatism in the meekest programmer. Suppose we have a list of three >items. Do we number them 1, 2, 3, or should it be 0, 1, 2?

Both ideas are completely stupid, there is only one err zero err 42 solutions.
42, 43, 44

Robert

--
I always knew that one day Smalltalk would replace Java.
I just didn't know it would be called Ruby
-- Kent Beck

Martin DeMello

7/16/2007 4:32:00 PM

0

On 7/16/07, Axel Etzold <AEtzold@gmx.de> wrote:
> > Raphael Gillett wrote the following on 16.07.2007 15:22 :
> > > [...]
> > > The pebble in the Ruby shoe is the counter-intuitive use of indexing of
> > > elements of arrays, strings, etc., from 0 to n-1 instead of the more
> > > natural 1 to n. Like prisoners who have got used to their chains, the
> > > Ruby community seems to have meekly accepted this impediment to clear
> > > thinking.
>
> Dear Raphael,
>
> from http://www.americanscientist.org/template/AssetDetail/assetid/51982?&...,
>
> an overview article about the history of around 6000 computer programming
> languages:
>
>
>
> "Still another perennially contentious issue is how to count. This one brings out the snarling
> dogmatism in the meekest programmer. Suppose we have a list of three items. Do we number
> them 1, 2, 3, or should it be 0, 1, 2? Everyone in computerdom knows the answer to that
> question, and knows it as an eternal truth held with the deepest, visceral conviction. Only one of
> the alternatives is logically tenable. But which is it ?.."

Should array indices start at 0 or 1? My compromise of 0.5 was
rejected without, I thought, proper consideration. -- Stan
Kelly-Bootle

martin