array of object insert polices

D

dave

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.


--
here are more things in heaven and earth,
horatio, than are dreamt of in your philosophy.
 
R

Robert Klemme

dave said:
hy list,

if i have a class as:

class IntegerArrray < Array
end

should i check if user inserts non Integer values ?

What other reason would there be for IntegerArray other than making it a
type safe array for integers?
if yes how?

def ensure_int(obj)
raise TypeError, "Not an int: #{obj.inspect}" unless Integer === obj
end

And call this whenever new elements enter the (<<, []= ...).
if not why?

Duck typing. Code that relies on ints in there will soon discover if
someone puts something non numeric in there.

Kind regards

robert
 
E

Eric Mahurin

--- dave said:
hy list,

if i have a class as:

class IntegerArrray < Array
end

should i check if user inserts non Integer values ?

If you are simply making a sub-class of Array, no.
if yes how?

I think a valid reason for restricting what goes into the array
would be efficiency - runtime or memory. In the case of
Fixnum, you only lose 1-bit - Fixnum is a 31-bit int using 32
bits of space. If you want an array of homogenous numbers,
narray allows you to store various sizes of ints, floats, and
complex numbers. I wouldn't mind having something that made a
homogenous array of whatever - mainly for memory efficiency.
if not why?

It may be useful to put things that look like and act like an
integer in this array. If you put something that doesn't act
like an integer in here, you'll find the problem when you take
it out of the array and try to use it like an integer. And if
you don't use it like an integer I see no need for any
restriction.






__________________________________
Discover Yahoo!
Find restaurants, movies, travel and more fun for the weekend. Check it out!
http://discover.yahoo.com/weekend.html
 
C

Charles Hixson

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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,772
Messages
2,569,588
Members
45,099
Latest member
AmbrosePri
Top