A Couple of Questions Regarding Ruby Style

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.

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. :slight_smile:

And welcome to Ruby.

- --
Phillip Gawlowski
Twitter: twitter.com/cynicalryan
Blog: http://justarubyist.blogspot.com

Zmodem has bigger bits, softer blocks, and tighter ASCII.

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.

It depends, mostly, on how loose you want to be. For example, when
dealing with strings, Integer only takes strings that represent an
integer value. So Integer('10'), Integer('0xA'), and Integer('012')
all work fine (and happen to represent the same value). However,
strings that don't JUST represent an integer raise an ArgumentError.
Integer('10.0') and Integer('10 eggs') fail.

String#to_i is much freer with what you can do. '10 eggs'.to_i gives
you 10, as does '10.1'.to_i. (Though, be aware that '10'.to_i !=
'0xA'.to_i)

Choosing between the two is simply a matter of deciding which behavior
is more desirable.

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?

If it will make the life of someone using this library easier to pass
in Integers, Floats, Strings, or Rationals (and I suspect that it
will), I bet allowing it will be worth the effort. And it is
perfectly acceptable for the initialize method call other methods for
conversion or whatever you need when a new instance is created.

HTH,
Chris

···

On May 15, 10:52 pm, "Michael W. Ryder" <_mwry...@worldnet.att.net> wrote:

Chris Shea wrote:

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.

It depends, mostly, on how loose you want to be. For example, when
dealing with strings, Integer only takes strings that represent an
integer value. So Integer('10'), Integer('0xA'), and Integer('012')
all work fine (and happen to represent the same value). However,
strings that don't JUST represent an integer raise an ArgumentError.
Integer('10.0') and Integer('10 eggs') fail.

String#to_i is much freer with what you can do. '10 eggs'.to_i gives
you 10, as does '10.1'.to_i. (Though, be aware that '10'.to_i !=
'0xA'.to_i)

I am really confused now! Integer('0xa') = 10 but '0xa'.to_i = 0. So it appears that "casting" using Integer(x) is preferable to x.to_i as it will convert 'valid' numbers. I also noticed that x = .25 is not allowed while x = Float(".25") or ".25"._to_f is.
The caveat about Integer(x) not handling '10 eggs' I can live with as I will ignore invalid data. The program currently converts strings to Floats using Float(x) then converts Floats to two Integers for the numerator and denominator. This allows the entry of Rational.new("7.5", ".25") to create a Rational number 30/1.

Choosing between the two is simply a matter of deciding which behavior
is more desirable.

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?

If it will make the life of someone using this library easier to pass
in Integers, Floats, Strings, or Rationals (and I suspect that it
will), I bet allowing it will be worth the effort. And it is
perfectly acceptable for the initialize method call other methods for
conversion or whatever you need when a new instance is created.

That will make it easier as I can create a method to handle strings and expand it as I go. I was thinking of adding support for strings such as "3/4" and then later when I figure out how to get the information for the locale allowing "1,234.35".
Thanks for clarifying these points. It looks like it is more important to make sure that the function being used does what you expect rather than what is most popular today.

···

On May 15, 10:52 pm, "Michael W. Ryder" <_mwry...@worldnet.att.net> > wrote:

HTH,
Chris

Float literals must have digits to the left of
the decimal to avoid ambiguity with method calls:

   x = 0.25

Gary Wright

···

On May 16, 2008, at 3:50 PM, Michael W. Ryder wrote:

I also noticed that x = .25 is not allowed

Gary Wright wrote:

I also noticed that x = .25 is not allowed

Float literals must have digits to the left of
the decimal to avoid ambiguity with method calls:

   x = 0.25

That I figured out a while ago. My comment was that x = .25 is not allowed but x = ".25".to_f or x = Float(".25") is. Many older languages allow x=.25 so having to place the leading zero is a sticking point as I am still not used to having to use it.
My main point was the "inconsistency" of a couple of methods that I thought were synonyms for each other. This is a real problem for me as I keep having to experiment to find the correct method and usage for the desired results. It took me a while to figure out how to get my coerce method to return Rational numbers rather than Floats. The examples in Pickaxe and in the rational.rb library all returned Floats. I finally figured out that rather than calling x.coerce(self) I had to call self.coerce(x). A simple change, like using Integer(x) rather than x.to_i for converting to Integers to avoid a problem with "0xa" returning 0 rather than the expected 10.

···

On May 16, 2008, at 3:50 PM, Michael W. Ryder wrote:

Gary Wright