[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Ruby stupidities

Kenneth McDonald

8/31/2007 3:25:00 AM

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


15 Answers

Austin Ziegler

8/31/2007 3:32:00 AM

0

On 8/30/07, Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net> wrote:
> 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.

It would be useful if you actually looked what's happening in the
future: this will be changed for Ruby 1.9. It is, in fact, a sensible
choice to make given the nature of strings in Ruby.

-austin
--
Austin Ziegler * halostatue@gmail.com * http://www.halo...
* austin@halostatue.ca * http://www.halo...feed/
* austin@zieglers.ca

Peña, Botp

8/31/2007 3:33:00 AM

0

From: Kenneth McDonald [mailto:kenneth.m.mcdonald@sbcglobal.net]
# "abc"[0] == "a" is false.

ruby is dynamic, and so is this community, and the change is good.

http://eigenclass.org/hiki.rb?Changes+in+Rub...

kind regards -botp



Mohit Sindhwani

8/31/2007 4:14:00 AM

0

Kenneth McDonald wrote:
> 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.

I admit that I've faced this one before :)

Cheers,
Mohit.
8/31/2007 | 12:13 PM.



James Britt

8/31/2007 5:14:00 AM

0

Kenneth McDonald wrote:

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

Not to rain on the Stupid Parade, but the ruby-talk and ruby-core list
archives (and the RCR archive, too) are full of such threads.

In the best cases, summary information gets written up on the Ruby
Garden wiki to help others on their way. If your particular issue is
not yet there, perhaps you could be Good Ruby Citizen and add it.

If you really want to make a point about one other other "stupid" Ruby
feature, please be sure to first read the prior threads.



--
James Britt

"Discover the recipes you are using and abandon them."
- Brian Eno and Peter Schmidt, Oblique Strategies

Damjan Rems

8/31/2007 5:40:00 AM

0


It is a matter of how you look at it:

irb(main):002:0> "abc"[0,1] == "a"
=> true


by
TheR

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

Michael Fellinger

8/31/2007 5:46:00 AM

0

On 8/31/07, Damjan Rems <d_rems@yahoo.com> wrote:
>
> It is a matter of how you look at it:
>
> irb(main):002:0> "abc"[0,1] == "a"
> => true

It's a matter that is fixed already, and waiting for you under the
christmas tree:

[manveru@pi ~]$ RUBYOPT= irb19
[RUBY_VERSION, RUBY_PLATFORM, RUBY_RELEASE_DATE]
# ["1.9.0", "i686-linux", "2007-08-28"]
"foo"[0]
# "f"
"foo"[0] == "f"
# true

^ manveru

Chris Game

8/31/2007 10:55:00 AM

0

On Fri, 31 Aug 2007 12:31:41 +0900, Austin Ziegler wrote:

> On 8/30/07, Kenneth McDonald <kenneth.m.mcdonald@sbcglobal.net> wrote:
>> 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.
>
> It would be useful if you actually looked what's happening in the
> future: this will be changed for Ruby 1.9. It is, in fact, a sensible
> choice to make given the nature of strings in Ruby.
>
> -austin

If it were a sensible choice, why change it? Sounds pretty stupid to
me!

--
Chris Game

"Not everything that can be counted counts, and not everything
that counts can be counted." - Albert Einstein

Matthias Wächter

9/3/2007 9:49:00 AM

0

On 31.08.2007 05:24, Kenneth McDonald wrote:
> 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"

You can change that behavior yourself without installing >=1.9

class String
alias :old_index :[]
def [](*a)
a.length==1 ? old_index(*a).chr : old_index(*a)
end
def asc # this is from [1]
case size
when 0; nil
when 1; old_index(0)
else unpack 'c*'
end
end
end

"abc"[0] == "a" # -> true
"abc"[1] == "b" # -> true
"abc"[0,1] == "a" # -> true
"a".asc == 97 # -> true
"abc".asc == [97,98,99] # -> true

But certainly this may break existing libs, so be careful.


[1] http://forum.ruby-portal.de/viewtopic.php?p...

- Matthias

dblack

9/3/2007 11:13:00 AM

0

John Joyce

9/3/2007 6:25:00 PM

0

Indeed, at some point you simply have to break things to do new or
better or different things. Endless backwards compatibility ends up
with bloated, hacked up junk.