From: Tim Pease Date: 2007-11-04T05:59:22+09:00 Subject: Re: dragons and factorials (keyboard input) On Nov 3, 2007, at 1:35 PM, Thufir wrote: > I'm getting keyboard input successfully, but I'm getting a string. I > need to convert it to a number. But ruby is a dynamic language? > What's happening please? > Others have already answered your question, and it appears you figured it out or yourself, too. Ruby does not perform coercion on strings. Perl allows you to do this ... 1 + "2" #=> 3 However, Matz decided to make type coercion the responsibility of the programmer and not the programming language. Whenever input is read (from a file or the command line) it is returned as a string. It is up to your program to convert that string into an Integer, a Float, and imaginary number, etc. In ruby you would need to write ... 1 + Integer("2") #=> 3 1 + "2".to_i #=> 3 1 + "2" #=> raises an error You can write your own coerce method for the string class, if you are so inclined, in order to make ruby strings behave like Perl strings. class String def coerce( other ) case other when Integer: [other, Integer(self)] when Float: [other, Float(self)] else raise TypeError, "can't coerce into '#{other.class.name}'" end end end WIth a coerce method defined for String, you can now write the following in Ruby ... 1 + "2" #=> 3 Blessings, TwP