M
Michael W. Ryder
While working on my rational class I came up with a couple of questions
regarding the accepted way to do things. I looked up a couple of Style
guides on the net but they seemed to cover only the formatting of code
and not what I was looking for.
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.
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? 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?
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.
regarding the accepted way to do things. I looked up a couple of Style
guides on the net but they seemed to cover only the formatting of code
and not what I was looking for.
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.
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? 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?
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.