From: David Masover Date: 2009-08-19T10:46:15+09:00 Subject: Re: Bizarre Range behavior On Monday 17 August 2009 08:46:25 am Scott Briggs wrote: > Piyush Ranjan wrote: > > +1 for that. and -1 for the change. > > > > It just makes the developer look too stupid. Can't we let the developers > > understand the difference between a string and an integer ? > > If it was 20 years ago, I'd understand this sentiment. What I don't > understand is why programming languages seem to insist on using > semantics that don't adapt to the natural ways that humans interact or > think. Because the semantics with which humans interact and think are ambiguous, often illogical, and often rely on intuition. We can't give our languages intuition, but the more we try to do so, and the more magic we introduce, the less predictable things get. > There are a lot of constructs in ruby that make it much easier to use > and understand from a natural language point of view, one of the big > strengths of ruby, and this in turn makes it more accessible to people > who are interested in programming and not getting bogged down in the > minutiae of why "2" is greater than "11". Programming inevitably leads to at least understanding these minutiae. I use Ruby, and I love it for that natural-language expressiveness, and also just for the conciseness, even where I know it's less efficient: (2..11).map(:&to_s) But there's a case to be made that at a certain point, you need to understand what's going on. A simple example: What's the difference between a string and a symbol? Someone who uses strings where they should use symbols is making their program needlessly inefficient and verbose; someone who does the opposite is introducing a rather serious memory leak and potential DoS vulnerability. You could make the case that we should just use strings, and find ways to make them really efficient. But hey, at least the semantics of symbols are adequately covered by strings -- the semantics of numbers really aren't. Put another way: Currently, we're allowed to do: puts 'Ho! '*3 + 'Merry Christmas!' Now, suppose we start making + and * smart, so that '2'*'3'='6'. Now what does '2'*3 do? Is it '6', or 6, or '222'? It certainly seems feasible a newbie would get stuck here -- for example, what if they feel like adding 000 as a delimiter -- '0'*80 instead of '-'*80 to make a horizontal line -- did they get eighty zeros, or the product of 0*80=0? Or suppose they added a space into their number accidentally -- is '2 '*80 equal to '160' or '2 2 2 2 ...'? Maybe it's just me, but '2 ' seems like a much more probable mistake (and a harder one to catch) than saying '2' when you mean 2. By making the easy stuff ridiculously easy (and assuming users are idiots), it adds enough ambiguity to drive users crazy later on. Maybe I'm overreacting, and this would be fine for ranges, but I think "magic" only makes sense when it's very well understood and predictable. 'puts' calling #to_s on everything, and 'p' calling #inspect on everything, makes sense. Range calling #to_i sometimes just seems like it's asking for trouble.