Ruby Profiler and ruby-prof Issues break working code?

I wrote some Ruby code to solve a little puzzle (
#14 Longest Collatz Sequence - Project Euler) that functions
correctly. It solves for the answer and gets it right, it’s just a
little
slower than I would like. So I thought I would bring out the old
profiler
and see what I could do to speed things up. Well… on the exact same
code
file, no changes besides ‘require “profile”’ it now gives me this error:

c:/ruby/lib/ruby/1.8/profiler.rb:13:in hash': can't convert Hash into Integer (TypeError) from c:/ruby/lib/ruby/1.8/profiler.rb:13:in []’
from c:/ruby/lib/ruby/1.8/profiler.rb:13
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:55:in initialize' from C:/Documents and Settings/Tyler P./Desktop/Project Euler/Problem14.rb:55:in initialize’
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:122:in new' from C:/Documents and Settings/Tyler P./Desktop/Project Euler/Problem14.rb:122:in find’
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:120:in each' from C:/Documents and Settings/Tyler P./Desktop/Project Euler/Problem14.rb:120:in find’
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:137

It seems like it is related to the my initialize method, but it is
simple
and correct:
def initialize(num)
@latest = num
@sequence = [@latest]
end

So I decided to abandon the standard profiler and try RubyProf instead.
Using #require ‘unprof’, it also keels over:

c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:33: warning:
ruby-prof: An error occured when leaving the method Kernel#require.
Perhaps an exception occured in the code being profiled?

And using require ‘ruby-prof’ and putting RubyProf.start and
RubyProf.stoparound the code I would like to profile results in:

C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:122:in initialize': undefined method %’ for
#Hash:0x2b8b9cc (NoMethodError)
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:122:in new' from C:/Documents and Settings/Tyler P./Desktop/Project Euler/Problem14.rb:122:in find’
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:120:in each' from C:/Documents and Settings/Tyler P./Desktop/Project Euler/Problem14.rb:120:in find’
from C:/Documents and Settings/Tyler P./Desktop/Project
Euler/Problem14.rb:137

I truly have no idea what is going on here. I’ve checked time and time
again, and this code works correctly without the profilers. I’ve ran it
with ruby -c -w, and it results in no warnings and the code is valid.
Is
the profiler trying to do something unusual here that I should be aware
of?

Thanks for any help, it is much appreciated.
–Tyler P.

Hi,

In message “Re: Ruby Profiler and ruby-prof Issues break working code?”
on Sun, 31 Dec 2006 19:28:39 +0900, “Tyler P.”
[email protected] writes:

|I wrote some Ruby code to solve a little puzzle (
|#14 Longest Collatz Sequence - Project Euler) that functions
|correctly. It solves for the answer and gets it right, it’s just a little
|slower than I would like. So I thought I would bring out the old profiler
|and see what I could do to speed things up. Well… on the exact same code
|file, no changes besides ‘require “profile”’ it now gives me this error:

This is very weird error. I have never seen something like this. It
is very difficult to help you without error reproducing code. Is it
possible for us to see your puzzle solving code that cause the
problem? We will very appreciate if you supply information for your
platform and the version of Ruby you’ve run.

						matz.

matz,

Thank you for the quick response. I don’t know which part of the code
the
problem is in, but I’m more than happy to let you look at the source
code.
It’s a little too long to post it all here, but here’s a link to it on
my
website.

Problem14.rb

I’m running ruby 1.8.5 (2006-08-25) on Windows XP SP2.
Thanks for your help,
Tyler P.

On 12/31/06, Yukihiro M. [email protected] wrote:

Hi,

You have redefined Chain.hash which is used internally in Hash to get
hash value, and should return integer value.

                                                    matz.

That solved it.

Thanks matz,
Tyler P.

Hi,

In message “Re: Ruby Profiler and ruby-prof Issues break working code?”
on Mon, 1 Jan 2007 08:21:01 +0900, “Tyler P.” [email protected]
writes:

|Thank you for the quick response. I don’t know which part of the code the
|problem is in, but I’m more than happy to let you look at the source code.
|It’s a little too long to post it all here, but here’s a link to it on my
|website.

You have redefined Chain.hash which is used internally in Hash to get
hash value, and should return integer value.

						matz.