# Ruby Profiler and ruby-prof Issues break working code?

**URL:** <https://www.ruby-forum.com/t/ruby-profiler-and-ruby-prof-issues-break-working-code/80860>\
**Category:** Ruby\
**Created:** [December 31, 2006, 11:30am UTC](https://www.ruby-forum.com/t/ruby-profiler-and-ruby-prof-issues-break-working-code/80860 "2006-12-31T11:30:13Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tyler\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@Tyler\_P](https://www.ruby-forum.com/u/Tyler_P)\
**Post date:** [December 31, 2006, 11:30am UTC](https://www.ruby-forum.com/t/ruby-profiler-and-ruby-prof-issues-break-working-code/80860/1 "2006-12-31T11:30:13Z")

</div>

I wrote some Ruby code to solve a little puzzle (  
[#14 Longest Collatz Sequence - Project Euler](http://projecteuler.net/index.php?section=problems&id=14)) 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.

---

<div class="post-metadata">

**Author:** ![Tyler\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@Tyler\_P](https://www.ruby-forum.com/u/Tyler_P)\
**Post date:** [December 31, 2006, 7:07pm UTC](https://www.ruby-forum.com/t/ruby-profiler-and-ruby-prof-issues-break-working-code/80860/2 "2006-12-31T19:07:06Z")

</div>

Hi,

In message “Re: Ruby Profiler and ruby-prof Issues break working code?”  
on Sun, 31 Dec 2006 19:28:39 +0900, “Tyler P.”  
[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) writes:

|I wrote some Ruby code to solve a little puzzle (  
|[#14 Longest Collatz Sequence - Project Euler](http://projecteuler.net/index.php?section=problems&id=14)) 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.

```

---

<div class="post-metadata">

**Author:** ![Tyler\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@Tyler\_P](https://www.ruby-forum.com/u/Tyler_P)\
**Post date:** [January 1, 2007, 12:22am UTC](https://www.ruby-forum.com/t/ruby-profiler-and-ruby-prof-issues-break-working-code/80860/3 "2007-01-01T00:22:12Z")

</div>

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](http://www.tylerprete.com/ruby/Problem14.rb)

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

---

<div class="post-metadata">

**Author:** ![Tyler\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@Tyler\_P](https://www.ruby-forum.com/u/Tyler_P)\
**Post date:** [January 1, 2007, 10:52pm UTC](https://www.ruby-forum.com/t/ruby-profiler-and-ruby-prof-issues-break-working-code/80860/4 "2007-01-01T22:52:13Z")

</div>

On 12/31/06, Yukihiro M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) 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.

---

<div class="post-metadata">

**Author:** ![Tyler\_P](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/5f9b8f/32.png) [@Tyler\_P](https://www.ruby-forum.com/u/Tyler_P)\
**Post date:** [January 1, 2007, 6:10am UTC](https://www.ruby-forum.com/t/ruby-profiler-and-ruby-prof-issues-break-working-code/80860/5 "2007-01-01T06:10:47Z")

</div>

Hi,

In message “Re: Ruby Profiler and ruby-prof Issues break working code?”  
on Mon, 1 Jan 2007 08:21:01 +0900, “Tyler P.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
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.

```
