# Ruby-prof doesn't work with ruby/tk?

**URL:** <https://www.ruby-forum.com/t/ruby-prof-doesnt-work-with-ruby-tk/80056>\
**Category:** Ruby\
**Created:** [December 21, 2006, 2:17am UTC](https://www.ruby-forum.com/t/ruby-prof-doesnt-work-with-ruby-tk/80056 "2006-12-21T02:17:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![fkc\_email-news\_S\_ya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@fkc\_email-news\_S\_ya](https://www.ruby-forum.com/u/fkc_email-news_S_ya)\
**Post date:** [December 21, 2006, 2:17am UTC](https://www.ruby-forum.com/t/ruby-prof-doesnt-work-with-ruby-tk/80056/1 "2006-12-21T02:17:06Z")

</div>

I’ve tried on several platforms and get the follow error with any tk  
code.

[Forrest@Mori ~/\<1\>masc\_log\_viewer/lib]$ ruby-prof tkcombobox.rb  
/usr/lib/ruby/gems/1.8/gems/ruby-prof-0.4.1/bin/ruby-prof:128:in  
`stop’: Unknown singleton class: 2145471700 (RuntimeError)  
from  
/usr/lib/ruby/gems/1.8/gems/ruby-prof-0.4.1/bin/ruby-prof:128

it’ll fail on a file that only has require ‘tk’ in it.

Is this a known problem? Any alternatives for profiling ruby/tk apps  
that are quicker than the built in profiler?

Thanks

Forrest

---

<div class="post-metadata">

**Author:** ![fkc\_email-news\_S\_ya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@fkc\_email-news\_S\_ya](https://www.ruby-forum.com/u/fkc_email-news_S_ya)\
**Post date:** [December 21, 2006, 9:27am UTC](https://www.ruby-forum.com/t/ruby-prof-doesnt-work-with-ruby-tk/80056/2 "2006-12-21T09:27:43Z")

</div>

From: “fkc\_email-news @ yahoo dot com” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
Subject: ruby-prof doesn’t work with ruby/tk?  
Date: Thu, 21 Dec 2006 10:15:09 +0900  
Message-ID: [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> I’ve tried on several platforms and get the follow error with any tk  
> code.
> 
> [Forrest@Mori ~/\<1\>masc\_log\_viewer/lib]$ ruby-prof tkcombobox.rb  
> /usr/lib/ruby/gems/1.8/gems/ruby-prof-0.4.1/bin/ruby-prof:128:in  
> `stop’: Unknown singleton class: 2145471700 (RuntimeError)  
> from  
> /usr/lib/ruby/gems/1.8/gems/ruby-prof-0.4.1/bin/ruby-prof:128  
> (snip)  
> Is this a known problem? Any alternatives for profiling ruby/tk apps  
> that are quicker than the built in profiler?

Please try the following patch.  
And run “ruby-prof -s tkcombobox.rb”.  
^^

diff -urN ruby-prof-0.4.1.orig/bin/ruby-prof  
ruby-prof-0.4.1.modify/bin/ruby-prof  
— ruby-prof-0.4.1.orig/bin/ruby-prof 2006-06-23 06:36:49.000000000  
+0900  
+++ ruby-prof-0.4.1.modify/bin/ruby-prof 2006-12-21 16:22:06.000000000  
+0900  
@@ -19,6 +19,7 @@

# wall - Use wall time.

# cpu - Use the CPU clock

counter

# (only supported on

Pentium and PowerPCs).  
+# -s, --set-program-name Set \<script.rb\> to $0.

# -h, --help Show help message

# –version Show version

# 

@@ -90,6 +91,11 @@  
end  
end

- 
- opts.on(‘-s’, ‘–set-program-name’, ‘Set \<script.rb\> to $0.’) do  
|mode|
- 

```
  options.set_progname = mode

```

- end
- opts.on\_tail(“-h”, “–help”, “Show help message”) do  
puts opts  
exit  
@@ -118,6 +124,56 @@  
exit(-1)  
end

+# replace $0 and $PROGRAM\_NAME  
+if options.set\_progname

- progname\_obj = Object.new
- progname\_obj.instance\_variable\_set(:@progname, $0)
- 
- class \<\< progname\_obj
- def **set\_progname** (name)
- 

```
 @progname = name.to_s

```

- end
- 
- def to\_str
- 

```
 @progname

```

- end
- alias to\_s to\_str
- alias inspect to\_str
- 
- def ===(other)
- 

```
 other === self.to_str

```

- end
- 
- def ==(other)
- 

```
 other == self.to_str

```

- end
- 
- def equal?(other)
- 

```
 other.equal?(self.to_str)

```

- end
- 
- def eql?(other)
- 

```
 other.eql?(self.to_str)

```

- end
- 
- def method\_missing(id, \*args)
- 

```
 self.to_str. __send__ (id, *args)

```

- end
- end
- 
- $program\_name = progname\_obj
- 
- alias $REAL\_PROGRAM\_NAME $0
- alias $PROGRAM\_NAME $program\_name
- alias $0 $program\_name
- 
- trace\_var(:$program\_name){|val|
- unless progname\_obj.object\_id == val.object\_id
- 

```
 progname_obj. __set_progname__ (val)

```

- 

```
 $program_name = progname_obj

```

- end
- }  
+end

# Install at\_exit handler. It is important that we do this

# before loading the scripts so our at\_exit handler run

@@ -146,6 +202,7 @@

# Get the script we will execute

script = ARGV.shift  
+$0 = script if options.set\_progname

# Start profiling

RubyProf.start  
diff -urN ruby-prof-0.4.1.orig/ext/ruby\_prof.c  
ruby-prof-0.4.1.modify/ext/ruby\_prof.c  
— ruby-prof-0.4.1.orig/ext/ruby\_prof.c 2006-06-23 06:36:49.000000000  
+0900  
+++ ruby-prof-0.4.1.modify/ext/ruby\_prof.c 2006-12-21 16:20:58.000000000  
+0900  
@@ -304,7 +304,7 @@  
result = rb\_str\_new2(“\<Unknown:”);  
rb\_str\_append(result, rb\_inspect(klass));  
rb\_str\_cat2(result, “\>#”);

- 

```
   rb_raise(rb_eRuntimeError, "Unknown singleton class: %i", 

```

result);

- 

```
   rb_warn("Unknown singleton class: %i", result);

```

}

return result;

---

<div class="post-metadata">

**Author:** ![fkc\_email-news\_S\_ya](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/bc79bd/32.png) [@fkc\_email-news\_S\_ya](https://www.ruby-forum.com/u/fkc_email-news_S_ya)\
**Post date:** [December 21, 2006, 11:25pm UTC](https://www.ruby-forum.com/t/ruby-prof-doesnt-work-with-ruby-tk/80056/3 "2006-12-21T23:25:58Z")

</div>

That worked for the most part. I had a failure with one chunk on the  
patch, but hand added that, and I had to add code to change foo.rb to  
./foo.rb for proper $0 substitution on my system (cygwin ruby).

Thanks for the quick and useful response. Here and all the other Tk  
questions you answer so quickly. Ever consider getting your Tk book  
translated?

Forrest
