On Fri, 27 Jul 2001 01:12:33 +0900, markus jais <info / mjais.de> wrote: > -------------------- > #!/usr/local/bin/ruby -w > > i = 1 > a = i + ARGV[0] > print a, "\n" > ------------------ > the error I get: > ./my.rb:4:in `+': String can't be coerced into Fixnum (TypeError) > from ./my.rb:4 > > Obviously the Perl Magic does not work in Ruby. > I have to write ARGV[0].to_i > > Does Ruby also support "Magic" as some Perl folks call this > that is, to convert variables automatically when used in a mathematical > expression. > if yes, how can I tell Ruby to be magic You can override the + operator on Strings and Fixnum (see below). But what result do you expect in the expression "1" + "4" (both Strings)? irb(main):020:0> 1 + "4" 5 irb(main):021:0> "2" + 3 5 irb(main):022:0> "1" + "4" "14" The last one might explain why Ruby isn't "magic". How do you differenciate concatenation and addition? You could 'add' strings if they are only digits, but how can you concat them then? You might need another operator (or method) to concat strings. Then you could have '+' to only add elements in the numeric way. But that's incompatible with other ruby scripts. --------- class Fixnum # should be more generic, but Numeric doesn't respond to :+ alias :_old_plus :+ def +(other) _old_plus other.to_i end end class String alias :_old_plus :+ def +(other) if other.is_a? Fixnum # Numeric? to_i + other else _old_plus other end end end ---------- Mike. midulo.