dave said:
hy list,
if i have a class as:
class IntegerArrray < Array
end
should i check if user inserts non Integer values ?
if yes how?
if not why?
tnx.
The key question is "What is the purpose of 'IntegerArray'?".
That determines both the answers to how and why.
Frequently I'm satisfied with merely ensuring that any value returned is
a valid integer. Sometimes, however, I would want to raise an exception
if the object being added did not respond to to_i, and at other times I
would want to insist that it actually BE an Integer (well, either a
Fixnum or a Bignum).
What is the PURPOSE? That's your keystone.
If you are writing an IntegerArray in a vacuum, then there is no basis
for making a decision...but were I to do so, I would insist that every
argument respond to ".to_i", which I would apply before saving it into
the array by any means whatsoever. (And I would consider that a foolish
exercise.)
That said, I frequently find reasons to create similar entities (though
never yet an IntegerArray). Generally the reason is so that I can
store them in, e.g., a file of fixed length blocks. (Note that this is
a reason external to Ruby.) I can also see doing this so that I could
more easily handle them in a chunk of c code. (I'm not sure that this
is a good reason...I haven't tried this yet.) But I'm always doing it
for some defined purpose. (And I often rethink the approach, and try
something else.)