The Pebble in the Ruby Shoe

R

Raphael Gillett

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).
 
S

SonOfLilit

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
 
L

Lionel Bouton

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

Sammy Larbi

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? =)
 
M

Michael Ulm

Raphael said:
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).
 
L

Lloyd Linklater

Raphael said:
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.
 
A

Axel Etzold

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?&print=yes,

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

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
 
J

John Joyce

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/EWD831.html

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

Bertram Scharpf

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
 
R

Robert Dober

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?&print=yes,

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
 
M

Martin DeMello

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?&print=yes,

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
 
T

Tim Pease

and we all know how 'natural' *those* languages are do program in
(ducks)...

Actually, if Ruby had been implemented in Fortran or Pascal, then most
certainly arrays would have begun indexing with 1 instead of 0.
However, since it was implemented using C, Ruby follows the convention
of implementation.

And we all know how easy it is to program in C ;-)

Blessings,
TwP
 
P

Pawe³ Kraszewski

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

I grew up with 0-based arrays. 1-based ones look unnatural to me. That's the
truth, as for me.

The worst nightmare for me was CHS addressing on disks - Cylinders are
0-based, Heads are 0-based, but Sectors are 1-based. Hell on earth.

That were my 3 cents.


PS.
O, and yes. Welcome everybody! I felt in love with Ruby 2 weeks ago. I find
it much more flexible than Python, my previous love and much more
expressive (and object-oriented) than PERL, my next-to-previous love. I
think Ruby puts the best of all scripting languages together. Gott sei dank
for Yukihiro Matsumoto and his precious mind.
 
X

Xeno Campanoli

Tim said:
Actually, if Ruby had been implemented in Fortran or Pascal, then most
certainly arrays would have begun indexing with 1 instead of 0.
However, since it was implemented using C, Ruby follows the convention
of implementation.

And we all know how easy it is to program in C ;-)

I always thought it made more sense to index from zero. After all, an
index represents an offset from the start, and the rest of the aspect is
just a one to one correspondence. The first thing there could as easily
be item Skattlebraught, the second being item Porktail, etc. The
standard of C using 0 as an index happened because it made sense, not
because it was merely different. I think Matz has typically adopted
things that make the most sense, and though some may not be used to
this, I don't find it unclear and it is more rational than starting with
index 1, though it may be less popular among some people. I think we
are always better off considering the adoption of something non-standard
that might be better. After all, if not, we would never have gotten
beyond COBOL and JCL, and we might all be living some "Brazil" Orwellian
nightmare.
 
L

Lloyd Linklater

Tim said:
Actually, if Ruby had been implemented in Fortran or Pascal, then most
certainly arrays would have begun indexing with 1 instead of 0.

Ok, I know that this is not a Ruby point, but I feel compelled to
respond. The basic array in Pascal is zero based.

var
ar: array of integer; // dynamic array
begin
SetLength(ar, 10); // makes a 0 - 9 array
end;

var
ar: array[1..10] of integer; // makes a 1 - 10 array

The default is pretty much zero based, even in Pascal.
 
M

matt neuburg

Raphael Gillett said:
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

But since Ruby lets you reach right in and override everything, couldn't
you just change this in your own programs if you don't like it?

m.
 
S

SonOfLilit

The pebble in the Ruby shoe is the counter-intuitive use of indexing of
But since Ruby lets you reach right in and override everything, couldn't
you just change this in your own programs if you don't like it?

Hmm, would probably break the standard library.

But you could inherit from Array.

Aur
 
T

Tom Werner

matt said:
But since Ruby lets you reach right in and override everything, couldn't
you just change this in your own programs if you don't like it?

m.

This seems like a terrible idea. Part of learning a language is learning
the ways that things are done in that language. If a coder starts
rewriting basic methods because of some silly prejudice against
zero-based indices, then that coder has just introduced a huge potential
trap for anyone wanting to modify that code. Some seriously nasty bugs
would almost certainly be introduced until the contributor realizes that
their basic assumptions about the language have been changed. Rewriting
functionality at that level should always be done with a very careful
analysis of how those changes might impact other coders, and the code
that other coders write.

Think before you reimplement.

Tom
 
J

John W. Kennedy

Pawe³ Kraszewski said:
I grew up with 0-based arrays. 1-based ones look unnatural to me. That's the
truth, as for me.

The worst nightmare for me was CHS addressing on disks - Cylinders are
0-based, Heads are 0-based, but Sectors are 1-based. Hell on earth.

On many disks, there is (or used to be) a sector #0 that is not used for
data; instead, it is used to record information about bad sectors, or
similar things.
--
John W. Kennedy
"But now is a new thing which is very old--
that the rich make themselves richer and not poorer,
which is the true Gospel, for the poor's sake."
-- Charles Williams. "Judgement at Chelmsford"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top