Undefined method?

When I press MSG or EXIT buttons I get undefined method errrs for the
programs domsg and doexit respectively. They are public methods so they
should be quite visible. Code follows - pls tell what I am doing wrong.
thanks in advance.

Joe

class MyForm
def domsg
puts “domsg called”
lbl.configure(“text”=>“Goodbye cruel world\nGoing down real soon”)
t=Time.now.asctime
t=t.split(" “)
puts t[0]+” “+t[1]+” “+t[2]+” “+t[3]
end
def doexit
puts “doexit called”
t=Time.now.asctime
t=t.split(” “)
puts t[0]+” “+t[1]+” “+t[2]+” "+t[3]
puts “…going down!..crash!”
exit
end
def initialize
ph= {‘padx’ =>10, ‘pady’ =>10}
root=TkRoot.new { title “NEAT!” }
@variable1 = TkVariable.new
@variable2 = TkVariable.new
top = TkFrame.new{root;(height 250;width 250)}
lbl=Tk::Tile::Label.new(top) {
text ‘Hello World! This is really neat stuff!’
}
lbl.place(‘height’ =>35,
‘width’ =>200,
‘x’ =>35,
‘y’ =>65
)
@entry1=TkEntry.new{top;textvariable @variable1; pack(ph)}
@entry1.place(‘height’ => 25,
‘width’ => 150,
‘x’ => 10,
‘y’ => 10)
@entry1.pack(ph)
@entry2=TkEntry.new{top;textvariable @variable2; pack(ph)}
@entry2.place(‘height’ => 25,
‘width’ => 150,
‘x’ => 10,
‘y’ => 40)
@entry2.pack(ph)
b1=TkButton.new(top) {text ‘MSG’ ;command {MyForm.domsg} ;pack ph}
b1.place(‘height’ =>25,
‘width’ =>100,
‘x’ =>50,
‘y’ =>100)
b2=TkButton.new(top) {text ‘EXIT’;command {MyForm.doexit};pack ph}
b2.place(‘height’ =>25,
‘width’ =>100,
‘x’ =>50,
‘y’ =>150)
top.pack(‘fill’=>‘both’, ‘side’ =>‘top’)
end
end
MyForm.new
tk.mainloop

I am not sure what this is, exactly, but you’ve written your callbacks
as:

b1=TkButton.new(top) {text ‘MSG’ ;command {MyForm.domsg} ;pack ph}
b2=TkButton.new(top) {text ‘EXIT’;command {MyForm.doexit};pack ph}

Note that you wrote “MyForm.domsg”. This says, “invoke the #domsg
method from the MyForm class”. However, you’ve defined #domsg as an
instance method, not a class method, so Ruby is right: #domsg is
undefined on the MyForm class.

~ jf

John F.
Principal Consultant, BitsBuilder
LI: http://www.linkedin.com/in/johnxf
SO: User John Feminella - Stack Overflow

how do I fix it?

On Thursday, September 8, 2011, Joe C. [email protected] wrote:

how do I fix it?

I think either def self.domsg or def MyForm.domsg may work.

Alan

that worked! - thanks much!

Joe