Gabriele, > Why this don't work: > > irb(main):001:0> a="strink" > => "strink" > irb(main):002:0> puts a+1 > TypeError: failed to convert Fixnum into String > from (irb):2:in `+' > from (irb):2 > > > > and this does: > > irb(main):003:0> puts a+1.to_s > strink1 > => nil > > It seems to me that "puts" works using .to_s on every > object , String.+ is not ? > > And.. if ruby knows that 1 is Fixnum, anf that it should > be translated to a String... what way it tryes to do this ? Your misunderstanding is in the definition of "every object". In your example, puts only sees 1 object: the result of the expression (a+1). For example: irb(main):001:0> a="strink" => "strink" irb(main):002:0> a+1 TypeError: failed to convert Fixnum into String from (irb):2:in `+' from (irb):2 You get the expected result if you pass the two parameters separately: irb(main):003:0> puts a,1 strink 1 => nil The same reasoning applies to ("strink"+=nil), ("strink"+=true), and (3+=nil). > (a=3, b=nil) > > irb(main):035:0> a+=b > TypeError: nil can't be coerced into Fixnum > from (irb):35:in `+' > from (irb):35 > > I still think 3+nil should simply be 3, but anyway, > why we've got a different result from those above? The reason you get a different error message for (3+=nil) is that ruby is trying to turn nil into a number instead of a string. > What's the difference beetween "convert" and "coerce" Since ruby has three built-in classes for numbers, ruby always tries to "coerce" a class into the desired numeric class. For example when ruby sees the expression (5.4 + 3), it tries to coerce the (3) - which is class Fixnum - into a Float. > (and, from "string+nil", we go to "nil+string") > > (NilQuestion "nil+") > > why nil does'nt have a "+" method? > It seem reasonable (and useful) having nothing > +something giving back nothing. I don't have a definitive answer to this, but I believe that nil is designed to represent "no object", even though it (like most everything else in ruby) actually is an object. Given this definition, I do not believe that nil should support any arithmetic methods. Also, this would lead to silent failures of expressions like (a = b + c) if b happened to be nil. > Why : > > irb(main):012:0> puts nil > nil > => nil > irb(main):013:0> puts nil.to_s > > => nil > > > It seems obvious that my previous assertion (puts using to_s) > is wrong, or we are always checking that an obj is not null ? This is a good question. puts should be calling to_s on all of its parameters, but for some reason, nil is being handled differently. Perhaps someone else can explain this inconsistency. - Warren Brown