だから私はこのアプリを持っています
require 'tk'
class Foo
def my_fancy_function
puts "hello, world!"
end
def initialize
@root = TkRoot.new{title "Hello, world!"}
frame = TkFrame.new
my_fancy_button = TkButton.new(frame) do
text "Press meee"
command {my_fancy_function}
pack
end
frame.pack
Tk.mainloop
end
end
bar = Foo.new
しかし、ボタンを押すと、「NameError:undefined local variable or method `my_fancy_function'for#<TkButton:...」が表示されます。
スコープに関連する些細なことが欠けていると確信しています...そのコマンドをボタンに正しくバインドするにはどうすればよいですか?
編集:わかりました、my_fancy_button
ブロックをパラメータに変更した場合、つまり
my_fancy_button = TkButton.new(frame, :text => "Press meee", :command => proc{my_fancy_function}).pack
その後、それは動作します。しかし、なぜ?