“2”.to_i *1.1
ArgumentError: illegal radix 1
from (irb):2:in `to_i’
from (irb):2
“2”.to_i * 1.1
=> 2.2
“2”.to_i *1.1 is parsed as “2”.to_i(*1.1). Try -w if you have doubt.
Yes, I figured it was something like that. But don’t you think that Ruby
should be able to figure that out?
I don’t know. I don’t think I’ve seen this problem in other high-level
languages. I guess that this is part of how I would expect a high-level
language to “stay out of my way” and let me focus on the problem I am
actually trying to solve.
···
–
Daniel Carrera
Graduate Teaching Assistant. Math Dept.
University of Maryland. (301) 405-5137
Your point about high-level languages is good, but this is the cost of
Ruby’s power. Most high-level languages are pretty simple and
gutless, making it easy to normalise the input.
Fortunately, as Matz said, the -w flag picks up things like this
easily. I use it as a habit.
The only time I really have trouble finding where a parse (or similar)
error occurs is when I’ve left out a closing bracket or something.
Gavin
···
On Friday, April 11, 2003, 5:26:48 PM, Daniel wrote:
“2”.to_i *1.1
ArgumentError: illegal radix 1
from (irb):2:in `to_i’
from (irb):2
“2”.to_i * 1.1
=> 2.2
“2”.to_i *1.1 is parsed as “2”.to_i(*1.1). Try -w if you have doubt.
Yes, I figured it was something like that. But don’t you think that Ruby
should be able to figure that out?
I don’t know. I don’t think I’ve seen this problem in other high-level
languages. I guess that this is part of how I would expect a high-level
language to “stay out of my way” and let me focus on the problem I am
actually trying to solve.
Well, I would normally expect * to be parsed differently (different
precedence). In particular, all of these:
ab, a b, a *b, a * b
Would be parsed as ‘(a)*(b)’. And if I want ‘a(*b)’ then that is
precisely what I would type.
In nearly all cases, when I use * I mean multiplication. For me, the use
‘a(*b)’ is so uncommon, that when that’s what I want I write it explicitly
to avoid ambiguity.
I would set the default parsing to (foo)*(1.1) in all cases, and only use
‘foo(*1.1)’ when explicitly stated.
Perhaps there’s a good reason why all this is a bad idea. But this
behaviour just seems much more natural to me.
···
On Fri, Apr 11, 2003 at 05:10:03PM +0900, Yukihiro Matsumoto wrote:
How do you think Ruby should figure out, when both
> In nearly all cases, when I use * I mean multiplication. For me, the use
> 'a(*b)' is so uncommon, that when that's what I want I write it explicitly
> to avoid ambiguity.
>
> I would set the default parsing to (foo)*(1.1) in all cases, and only use
> 'foo(*1.1)' when explicitly stated.
For me it’s the other way around. I have yet to use any multiplication
in this object system, but I use *args quite a bit. Everything from
argument list forwarding to widget parameter passing:
o = Class.new_on(connection, :volume, arg0, …)
r = Row.new(1, 2, 3) # Row#new declares with *args
or
a = [1, 2, 3]
do stuff to a
r = Row.new *a
It all depends on context, and unfortunately there’s no way for ruby
to know the context. I believe it’s just the same thing as the "1+ 1"
problem. Besides, if your code looks like that, you really should make
it look nicer.