# Click can't run my class

**URL:** <https://www.ruby-forum.com/t/click-cant-run-my-class/191175>\
**Category:** Ruby-Gnome 2\
**Created:** [July 7, 2010, 5:08am UTC](https://www.ruby-forum.com/t/click-cant-run-my-class/191175 "2010-07-07T05:08:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [July 7, 2010, 5:08am UTC](https://www.ruby-forum.com/t/click-cant-run-my-class/191175/1 "2010-07-07T05:08:31Z")

</div>

there is simple class,it can run,i have tested it  
class Myfile  
def self.openit(myfile)  
file=open(myfile,‘r’)  
while line=file.gets  
puts line  
end  
file.close  
end  
end  
Myfile.openit("/home/pt/code")

when i add it into gtk ,it can’t work.  
here is my program:  
require ‘gtk2’  
button = Gtk::Button.new(“my ruby”)  
button.signal\_connect(“clicked”) {  
Myfile.openit("/home/pt/code")  
}  
window = Gtk::Window.new  
window.signal\_connect(“delete\_event”) {  
puts “delete event occurred”  
false  
}  
window.signal\_connect(“destroy”) {  
puts “destroy event occurred”  
Gtk.main\_quit  
}  
window.border\_width = 10  
window.add(button)  
window.show\_all  
Gtk.main

class Myfile  
def self.openit(myfile)  
file=open(myfile,‘r’)  
while line=file.gets  
puts line  
end  
file.close  
end  
end

the outout is:

pt@pt-laptop:~$ ruby /home/pt/t10.rb  
/home/pt/t10.rb:4: uninitialized constant Myfile  
from /home/pt/t10.rb:27:in `call' from /home/pt/t10.rb:27:in`main’  
from /home/pt/t10.rb:27  
how to revise it?

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [July 7, 2010, 10:39am UTC](https://www.ruby-forum.com/t/click-cant-run-my-class/191175/2 "2010-07-07T10:39:14Z")

</div>

Ruby code is executed as it is reached - and class definitions are ruby  
code.

Your code is laid out in the form:

which means that the class ‘Myfile’ doesn’t get defined until the  
program  
has finished running.

To fix it, you’ll need to move the Myfile class to the top of the file  
so  
that the class gets defined before you try to use it.

HTH,

G.

---

<div class="post-metadata">

**Author:** ![luofeiyu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de0a6/32.png) [@luofeiyu](https://www.ruby-forum.com/u/luofeiyu)\
**Post date:** [July 7, 2010, 2:58pm UTC](https://www.ruby-forum.com/t/click-cant-run-my-class/191175/3 "2010-07-07T14:58:51Z")

</div>

i fixed it,it can run now.think you  
class Myfile  
def self.openit(myfile)  
file=open(myfile,‘r’)  
while line=file.gets  
puts line  
end  
file.close  
end  
end  
require ‘gtk2’  
button = Gtk::Button.new(“my ruby”)  
button.signal\_connect(“clicked”) {  
Myfile.openit("/home/pt/code")  
}  
window = Gtk::Window.new  
window.signal\_connect(“delete\_event”) {  
puts “delete event occurred”  
false  
}  
window.signal\_connect(“destroy”) {  
puts “destroy event occurred”  
Gtk.main\_quit  
}  
window.border\_width = 10  
window.add(button)  
window.show\_all  
Gtk.main
