Ruby(Shoes)-等待函数返回值

Ruby(Shoes)-等待函数返回值,ruby,function,shoes,Ruby,Function,Shoes,我有一个向用户显示组合框的函数 def select_interface(interfaces) list_box :items => interfaces do |list| interface = list.text end ### ideally should wait until interface has a value then: ### return interface end 程序的其余部分取决于此组合框中的选择 def select_interf

我有一个向用户显示组合框的函数

def select_interface(interfaces)
  list_box :items => interfaces do |list|
    interface = list.text
  end
  ### ideally should wait until interface has a value then: ###
  return interface
end
程序的其余部分取决于此组合框中的选择

def select_interface(interfaces)
  list_box :items => interfaces do |list|
    interface = list.text
  end
  ### ideally should wait until interface has a value then: ###
  return interface
end
我想找到一种方法,让ruby等待来自组合框的输入,然后执行其余的代码

中有一个类似的函数,它将等待用户的输入

interface =  ask("write your interface here")

我如何在Ruby/shoes中实现这个“等待变量有值”函数?

我花了一段时间才理解你的问题:)我开始写一个关于GUI应用程序整个理论的长答案。但是你已经拥有了你所需要的一切。所需的块实际上是其更改方法。你在告诉它当它被改变时该怎么做。当您得到所需的值时,只需将程序的其余部分推迟运行即可

Shoes.app do 
  interfaces = ["blah", "blah1", "blah2"]
  # proc is also called lambda
  @run_rest_of_application = proc do
    if @interface == "blah"
      do_blah
    # etc
  end

  @list_box = list_box(:items => interfaces) do |list|
    @interface = list.text
    @run_rest_of_application.call
    @list_box.hide # Maybe you only wanted this one time?
  end
end
这是所有GUI应用程序背后的基本思想:构建初始应用程序,然后等待“事件”,这将为您创建新的响应状态。例如,在ruby-gnome2中,您将使用一个回调函数/块来更改应用程序的状态。大概是这样的:

# Let's say you're in a method in a class
@interface = nil
@combobox.signal_connect("changed") do |widget| 
  @interface = widget.selection.selected
  rebuild_using_interface
end
即使在工具包之外,您也可以使用Ruby的“免费”事件系统。希望这有帮助