From: "Harald Große" Date: 2002-01-26T21:51:45+09:00 Subject: Re: Proc question (with Gtk Menus) Hello, to call function from menu and other methods, I use the following code: $menu = [ [ "/_File", "", "", 0 ], [ "/File/toff", "", "", 0 ], [ "/File/_Open", "O", "", "file_open" ], [ "/File/sep1", "", "", 0 ], [ "/File/_End", "E", "", "file_end" ], ...... ] class Program ...... def buildMenu ...... wbox = Gtk::VBox.new(false, 0) # menubar accel = Gtk::AccelGroup.new mbar = Gtk::ItemFactory.new(Gtk::ItemFactory::TYPE_MENU_BAR, "
", accel) mfac = [] $menu.each { |m| mfac.push [ m[0], m[1], m[2], (m[3] == 0) ? nil : roc{ method(m[3]).call(0) } ] } mbar.create_items(mfac) wbox.pack_start(mbar.get_widget("
"), false, false, 0) ....... end # menu methods def file_open(mod) ... end def file_end(mod) Gtk.main_quit end ...... end harald >-----Urspr�ngliche Nachricht----- >Von: Markus Jais >Newsgroups: comp.lang.ruby >An: ruby-talk ML ; undisclosed-recipients: ; >Datum: Samstag, 26. Januar 2002 12:53 >Betreff: Proc question (with Gtk Menus) > >hello > >yesterday someone posted this code in response to one of my questions: > >require "gtk" >class SampleWindow < Gtk::Window > def pn(msg) > return Proc.new {puts msg,"\n"} > end > def initialize > super > items=[ > ["/_File", nil, "", nil, nil], > ["/File/_New", "N", "", pn("New"), nil], > ["/File/_Quit", "Q", "", pn("Quit", nil], >....... > >the 4th argument in the array has to be a function which returns a Proc > >lets say I want to call this function also in another place and there it >should be a normal function which does not just return a Proc object. > >Is this possible?? > >when I change the function to >def pn(msg) > puts msg >end > >then the function always gets executed when I puts it in the array > >maybe I am just blind or haven't understand how this Proc stuff really >works? > >please help. this drives me crazy :-) > >markus