From: "trans. (T. Onoma)" Date: 2004-10-25T01:48:42+09:00 Subject: Re: [rcr] String#first / String#last On Sunday 24 October 2004 12:12 pm, ts wrote: | >>>>> "t" == trans (T Onoma) writes: | | t> On Sunday 24 October 2004 09:52 am, Simon Strandgaard wrote: | | t> | unless n.kind_of?(Integer) | t> | raise TypeError, "#{length.class}#to_int should return Integer" | t> | end | | t> I don't think this is really needed, since such a bug would be worked | out else t> where. (That #to_int might be used usefully for something other | then t> returning an integer is extremely unlikely and thus not worth the | check). | | svg% ruby -e 'class A; def to_int() "a" end end; [1,2].first(A.new)' | -e:1:in `first': A#to_int should return Integer (TypeError) | from -e:1 | svg% I see --to behave like Array's methods. Seems like a lot overhead though when one considers this kind of thing is going on throughout the system. I added to my lib, but I did this instead: begin n = n.to_int.abs rescue NoMethodError raise TypeError, "cannot convert #{length.class} to positive Integer" end which allowed me to get rid of the negative check too. In the very very very unlikely case that a non-integer slips through (a String for instance) one still just gets: ArgumentError: comparison of Fixnum with String failed T.