Ruby stupidities



The title is intentionally (but semiseriously) contentious, but I'm not trying to say that Ruby is a bad language--I'm using it, right :-) ? However, any language has its unfortunate share of bad APIs, design decisions, etc., and Ruby is no exception. I've stumbled across a few, and thought it might be useful to start up a thread discussing what people view as Ruby's stupidities. Why? Because it's a lot less painful to find out about them by reading them than be tracking obscure errors in one's code.

In order to keep this on a not-completely-shouting-match level, I think it's fair to give a reason something you mention as a Ruby Stupidity is in fact stupid. For example, it violates common sense, it causes more trouble than it's worth, etc. etc., and to discuss what a better way of implementing such a feature might be.

Having said that, here's my first entry. I think it's utterly stupid (can you tell I just wasted some time tracking this down?) that

"abc"[0] == "a"

is false. Why doesn't that work? Because, with a single index, the array access operator on a string returns, not the character at the given position, but the _character code_ of the character at that position.

And why is that stupid?
1) It's inconsistent even in Ruby's own String API; all other index operations (at least as shown in the standard rdoc) give strings or nil.
2) It's unusual compared to most other scripting languages, meaning it makes Ruby less approachable.
3) There seems to be absolutely no reason to do things this way; providing a 'char_code' string method would result in clearer programs, and it's not like converting a character to a char code is such a common operation that it's necessary to save a few keystrokes at the cost of unclear code.

Here's hoping someone out there avoids this mistake after reading this.


cheers,
Ken


.



Relevant Pages

  • Re: ENTER character
    ... carriage return character, ASCII value 13, represented in a Ruby ... On *nix-variants, pressing the key ... Ruby string by "\n". ...
    (comp.lang.ruby)
  • Re: Question regarding design of the String Class
    ... string, ... > integer for the character at position i, exactly as in Ruby. ... 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. ...
    (comp.lang.ruby)
  • Re: Why was the "Symbol is a String"-idea dropped?
    ... I want a Ruby that just works - crystal-clear, transparently, reliably. ... 10MB+1byte of binary dump, and the old 10MB object is garbage-collected. ... there are cases where it is just so *useful* to append to a string. ... because you'd have to look up the symbol ID to convert it into its character ...
    (comp.lang.ruby)
  • Re: Ruby stupidities
    ... However, any language has its unfortunate share of bad APIs, design decisions, etc., and Ruby is no exception. ... In order to keep this on a not-completely-shouting-match level, I think it's fair to give a reason something you mention as a Ruby Stupidity is in fact stupid. ... Because, with a single index, the array access operator on a string returns, not the character at the given position, but the _character code_ of the character at that position. ...
    (comp.lang.ruby)
  • Re: Ruby stupidities
    ... access operator on a string returns, not the character at the given ... but the _character code_ of the character at that position. ... choice to make given the nature of strings in Ruby. ...
    (comp.lang.ruby)