Ruby(鞋子)- 等待函数返回值

发布于 2024-07-09 21:09:27 字数 630 浏览 9 评论 0原文

我有一个向用户呈现组合框的功能。

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​​ 等待组合框的输入,然后执行其余的代码。

shoes 中有一个类似的功能,称为 ask 将等待用户的输入。

interface =  ask("write your interface here")

如何在 Ruby/shoes 中实现“等待变量有值”功能?

I have a function that presents the user a combo-box.

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

The rest of the program depends on the selection from this combo-box.

I would like to find a way to make ruby wait for the input from the combo-box and then carry executing the rest of the code.

There is a similar function in shoes called ask that will wait for the input of the user.

interface =  ask("write your interface here")

How can I implement this "wait until the variable has a value" function in Ruby/shoes?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

得不到的就毁灭 2024-07-16 21:09:27

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

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 中,您可以使用带有 Gtk::ComboBox 会改变你的应用程序的状态。 像这样的事情:

# 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 的 观察者模块。 希望这有帮助。

It took me a while to understand your question :) I started writing a long answer about the entire theory of GUI applications. But you already have everything you need. The block that list_box takes is really its change method. You're telling it what to do when it gets changed. Just defer the rest of the program to run when you get a value you want.

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

This is the basic idea behind all GUI applications: build the initial application then wait for "events", which will create new states for you respond to. In ruby-gnome2, for example, you would use a callback function/block with a Gtk::ComboBox that would change the state of your application. Something like this:

# 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

Even outside of a toolkit you can get a "free" events system using Ruby's Observer module. Hope this helped.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文