From: "Mauricio Fernández" Date: 2004-07-03T08:28:51+09:00 Subject: Re: slow method searching? On Sat, Jul 03, 2004 at 08:02:57AM +0900, Mauricio Fern�ndez wrote: > > Why does it take longer and longer for Ruby to throw NoMethodError for > > large arrays? What is going on here? This happens with hash too. > > When the exception is printed, a string representation of the associated > object is created by calling #inspect. See name_err_mesg_to_str in > error.c: [...] > BTW, one oddity in the implementation: ruby uses internally a constant > named "message" (lower case), which you cannot access from Ruby neither as > NameError::message nor with NameError.const_get("message"), however > > >> ObjectSpace.each_object(Class){|x| p x if x.ancestors.include? Data} > NameError::message > Data Just for a laugh, here's some additional confirmation of the above: batsman@tux-chan:/tmp$ cat dfghdfg.rb def test_exc(code) s = Time.new eval code rescue NameError puts "Eval: #{code}" puts "Message: #{$!.message}" puts "Took #{Time.new-s} seconds." end test_exc %{ a=(1..400000).to_a; a.classx } test_exc %{ a=(1..400000).to_a; b=[]; b.classx } puts "=" * 80 puts "Redefining #to_str for exception messages..." puts klass = nil ObjectSpace.each_object(Class){|c| klass = c if c.ancestors.include?(Data) && c!= Data} klass.send(:define_method, :to_str) { "booooo spooky NameError" } test_exc %{ a=(1..400000).to_a; a.classx } test_exc %{ a=(1..400000).to_a; b=[]; b.classx } batsman@tux-chan:/tmp$ ruby dfghdfg.rb Eval: a=(1..400000).to_a; a.classx Message: undefined method `classx' for # Took 1.691099 seconds. Eval: a=(1..400000).to_a; b=[]; b.classx Message: undefined method `classx' for []:Array Took 0.106893 seconds. ================================================================================ Redefining #to_str for exception messages... Eval: a=(1..400000).to_a; a.classx Message: booooo spooky NameError Took 0.115443 seconds. Eval: a=(1..400000).to_a; b=[]; b.classx Message: booooo spooky NameError Took 0.110813 seconds. -- Running Debian GNU/Linux Sid (unstable) batsman dot geo at yahoo dot com Debian is like Suse with yast turned off, just better. :) -- Goswin Brederlow