[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.ruby

Re: array of object insert polices

Peña, Botp

6/1/2005 8:07:00 AM

dave [mailto: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 ?

imho, yes.

#
#if yes how?

maybe something like this? (this is just a sample),

c:\family\ruby>irb
irb(main):001:0> system "type a1.rb"
class IA < Array
end

class IA
def << (other)
p "You entered #{other}"
if other.class == Fixnum
p "That's an integer you're inserting. go!"
super # call original insert method <<
else
p "nope: i'm sorry, only integers allowed"
end
end
end


=> true
irb(main):002:0> require "a1.rb"
=> true
irb(main):003:0> x = IA.new
=> []
irb(main):004:0> x << 1
"You entered 1"
"That's an integer you're inserting. go!"
=> [1]
irb(main):005:0> x << 1.5
"You entered 1.5"
"nope: i'm sorry, only integers allowed"
=> nil
irb(main):006:0> x << "a"
"You entered a"
"nope: i'm sorry, only integers allowed"
=> nil
irb(main):007:0> x << 0
"You entered 0"
"That's an integer you're inserting. go!"
=> [1, 0]
irb(main):008:0> x << -1
"You entered -1"
"That's an integer you're inserting. go!"
=> [1, 0, -1]
irb(main):009:0> p x
[1, 0, -1]
=> nil
irb(main):010:0>

thanks and kind regards -botp

#if not why?
#
#tnx.


1 Answer

dave

6/1/2005 8:55:00 AM

0



irb(main):006:0> x << "a"
"You entered a"
"nope: i'm sorry, only integers allowed"

--------

and what if an user does x[2] = "i'm an integer :)"



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