R. Kumar wrote:
> If i use something like:
> 
>>    array.count rescue array.size
> 
> does the rescue set up anything internally that will make this
> inefficient ?

rescue's impact is negligible.

HOWEVER, that's NOT the question you need to ask. You need to ask how 
costly is hitting on a nonexistent method. See my benchmark (I do have 
Array#size on my system):

Benchmark.bm {|bm| bm.report {100000.times {a.size rescue a.size}}}
      user     system      total        real
  0.110000   0.000000   0.110000 (  0.219765)

Benchmark.bm {|bm| bm.report {100000.times {a.not_found rescue a.size}}}
      user     system      total        real
 30.450000   0.520000  30.970000 ( 33.876875)

See? On my system, hitting a nonexistent method is 300 times slower than 
existing one.

> Other options are :
> 
> unless Array.method_defined?(:count) then
>   class Array
>     def count
>       size
>     end
>   end
> end

It'd be faster if you use the 'alias' keyword instead of wrapping the 
call in a function of your own because then you save one function call.
-- 
Posted via http://www.ruby-forum.com/.