[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

array of object insert polices

dave

6/1/2005 7:29:00 AM



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.


27 Answers

Robert Klemme

6/1/2005 9:11:00 AM

0

dave wrote:
> 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

Eric Mahurin

6/1/2005 1:17:00 PM

0


--- dave <dave.m@email.it> wrote:

>
>
> 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/we...



Charles Hixson

6/1/2005 10:00:00 PM

0

dave wrote:

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



Mike Smith

7/23/2011 5:16:00 PM

0

On Sat, 23 Jul 2011 09:44:48 -0700, Bible Studies with Satan
<bible@hotmail.com> wrote:

>Tracey12 wrote:
>
>> You cannot stop a determined person. Anti-Gun laws don't work. In the
>> USA, the states and cities that have the most rigid, likely
>> unconstitutional gun laws have the highest murder rates. Anti-gun laws
>> don't stop criminals who care nothing about law and order. Anti-gun
>> laws only make certain that the victims are unarmed when the attacker
>> begins to shoot.
>
>The attacker/terrorist is a Christian Conservative.

No he's not. He has been linked to the far left wing nazi party.

Mike Smith

wy

7/23/2011 5:35:00 PM

0

On Jul 23, 1:16 pm, Mike Smith <m...@wt.net> wrote:
> On Sat, 23 Jul 2011 09:44:48 -0700, Bible Studies with Satan
>
> <bi...@hotmail.com> wrote:
> >Tracey12 wrote:
>
> >> You cannot stop a determined person. Anti-Gun laws don't work. In the
> >> USA, the states and cities that have the most rigid, likely
> >> unconstitutional gun laws have the highest murder rates. Anti-gun laws
> >> don't stop criminals who care nothing about law and order. Anti-gun
> >> laws only make certain that the victims are unarmed when the attacker
> >> begins to shoot.
>
> >The attacker/terrorist is a Christian Conservative.
>
> No he's not. He has been linked to the far left wing nazi party.

There's a difference?

The PHANTOM

7/23/2011 6:04:00 PM

0

On Jul 23, 11:44 am, Bible Studies with Satan <bi...@hotmail.com>
wrote:
> Tracey12 wrote:
> > You cannot stop a determined person. Anti-Gun laws don't work. In the
> > USA, the states and cities that have the most rigid, likely
> > unconstitutional gun laws have the highest murder rates. Anti-gun laws
> > don't stop criminals who care nothing about law and order. Anti-gun
> > laws only make certain that the victims are unarmed when the attacker
> > begins to shoot.
>
> The attacker/terrorist is a Christian Conservative.
>
Suuuurrrre he is. (wink-nudge)


> --
> Ezekiel 23:20

Tracey12

7/23/2011 6:23:00 PM

0

On Jul 23, 12:16 pm, Mike Smith <m...@wt.net> wrote:

>
> >The attacker/terrorist is a Christian Conservative.
>
> No he's not. He has been linked to the far left wing nazi party.
>
> Mike Smith

The God hating left is ready to jump when a great tragedy may have
been linked to a Conservative, particularly the most hated among
Conservatives, the Christian Conservative.
Yet, the story is not fully been told. We don't know for certain what
this Norwegian is.

Just like in Arizona, the immediate response from the commie left was
that Jared Loughner was a right wing extremist. He was not. He is a
certified nut and isn't facing prosecution now because of his
nuttiness. However, this won't stop the God hating left from trying
to label all Conservatives and especially Christian Conservatives.

wy

7/23/2011 6:36:00 PM

0

On Jul 23, 2:22 pm, Tracey12 <tracey12em...@gmail.com> wrote:
> On Jul 23, 12:16 pm, Mike Smith <m...@wt.net> wrote:
>
>
>
> > >The attacker/terrorist is a Christian Conservative.
>
> > No he's not. He has been linked to the far left wing nazi party.
>
> > Mike Smith
>
> The God hating left is ready to jump when a great tragedy may have
> been linked to a Conservative, particularly the most hated among
> Conservatives, the Christian Conservative.
> Yet, the story is not fully been told.  We don't know for certain what
> this Norwegian is.
>
> Just like in Arizona, the immediate response from the commie left was
> that  Jared Loughner was a right wing extremist.  He was not.  He is a
> certified nut and isn't facing prosecution now because of his
> nuttiness.  However, this won't stop the God hating left from trying
> to label all Conservatives and especially Christian Conservatives.

Certified nut, right wing extremist... what's the difference?

Bible Studies with Satan

7/23/2011 7:10:00 PM

0

Tracey12 wrote:

> On Jul 23, 12:16?pm, Mike Smith <m...@wt.net> wrote:
>
>>
>> >The attacker/terrorist is a Christian Conservative.
>>
>> No he's not. He has been linked to the far left wing nazi party.
>>
>> Mike Smith
>
> The God hating left is ready to jump when a great tragedy may have
> been linked to a Conservative, particularly the most hated among
> Conservatives, the Christian Conservative.

That's what his own Facebook said.

> Yet, the story is not fully been told.

Yes, it has. Christian conservatives are a threat to our safety.

> We don't know for certain what
> this Norwegian is.
>
> Just like in Arizona, the immediate response from the commie left was
> that Jared Loughner was a right wing extremist. He was not. He is a
> certified nut and isn't facing prosecution now because of his
> nuttiness. However, this won't stop the God hating left from trying
> to label all Conservatives and especially Christian Conservatives.

--
Ezekiel 23:20

Mike Smith

7/23/2011 7:19:00 PM

0

On Sat, 23 Jul 2011 10:34:35 -0700 (PDT), wy <wy_@myself.com> wrote:

>On Jul 23, 1:16?pm, Mike Smith <m...@wt.net> wrote:
>> On Sat, 23 Jul 2011 09:44:48 -0700, Bible Studies with Satan
>>
>> <bi...@hotmail.com> wrote:
>> >Tracey12 wrote:
>>
>> >> You cannot stop a determined person. Anti-Gun laws don't work. In the
>> >> USA, the states and cities that have the most rigid, likely
>> >> unconstitutional gun laws have the highest murder rates. Anti-gun laws
>> >> don't stop criminals who care nothing about law and order. Anti-gun
>> >> laws only make certain that the victims are unarmed when the attacker
>> >> begins to shoot.
>>
>> >The attacker/terrorist is a Christian Conservative.
>>
>> No he's not. He has been linked to the far left wing nazi party.
>
>There's a difference?

You do not possess the intelligence required to understand the
difference.

Mike Smith