M
Michael W. Ryder
I am part way through implementing a Rational math class to further my
understanding of Ruby and had a couple of questions that I can't find
the answer to in Pickaxe.
The first question regards creating a new instance of the class. The
initialize method expects two integer values. While I have no problem
making sure that they are integers I am not sure what to do if they are
not. Do I return nil or some other result?
The other question is how to override basic math operations like
addition and multiplication. I could implement them as x.add(y) but
would prefer to just be able to enter x + y. I think I have to create a
new instance of the class for the result and return that but am not sure
how to make it so that Ruby calls the right method when it sees x + y.
On a related note is there any good source for writing operations like
the math and probably coerce? If I can get the math working the
comparable operations should be "trivial".
understanding of Ruby and had a couple of questions that I can't find
the answer to in Pickaxe.
The first question regards creating a new instance of the class. The
initialize method expects two integer values. While I have no problem
making sure that they are integers I am not sure what to do if they are
not. Do I return nil or some other result?
The other question is how to override basic math operations like
addition and multiplication. I could implement them as x.add(y) but
would prefer to just be able to enter x + y. I think I have to create a
new instance of the class for the result and return that but am not sure
how to make it so that Ruby calls the right method when it sees x + y.
On a related note is there any good source for writing operations like
the math and probably coerce? If I can get the math working the
comparable operations should be "trivial".