From: Phillip Gawlowski Date: 2008-05-16T14:20:21+09:00 Subject: Re: A Couple of Questions Regarding Ruby Style -----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Michael W. Ryder wrote: | The first is when casting is it preferred to use Integer(x) or x.to_i? | Being from a C background the first is more obvious to me as it states | that I am converting x to an Integer. The second has its place when | chaining several changes to a variable but I can't see it as being clearer. #to_i should be preferred. It's way more commonly used in Ruby code than Integer(x), and it is, IMO, more readable: 'x to integer'. | The second involves the initialization method for a class. I am making | it so that the .new method will accept Integers, Floats, Strings, and | Rational numbers. Should I be doing this, or should I require that all | the converting be done before calling new? Depends. If it is a class that isn't exposed, you should convert before you initialize the class. Otherwise, handle the conversion yourself. That's how I'd approach it. | If I allow any type of | Numeric, or String representation, is it acceptable to have the method | call other methods for some of the conversions? Yes. | I am learning a lot about Ruby in this project, and it seems that I am | constantly finding new things to add to it, and would like to code it in | the accepted style. The most accepted style is the style you are comfortable with. Sooner or later, you'll join the Dark Side^H^H^H^H^H^H^H^H^HRuby way of things, by reading code, etc. Which will help with finding out what the general style is, too. Read code, and look at how others do things. :) And welcome to Ruby. - -- Phillip Gawlowski Twitter: twitter.com/cynicalryan Blog: http://justarubyist.blogspot.com Zmodem has bigger bits, softer blocks, and tighter ASCII. -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.8 (MingW32) Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org iEYEARECAAYFAkgtGZIACgkQbtAgaoJTgL+7WgCeOe36QhHh0jt6T7J+Y78cO9j2 nKYAoJh2LXVBapZoAVuOmC4Emfu0xWwP =psqh -----END PGP SIGNATURE-----