Ruby(鞋子)- 等待函数返回值
我有一个向用户呈现组合框的功能。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我花了一段时间才理解你的问题:)我开始写一个关于 GUI 应用程序的整个理论的长答案。 但你已经拥有了你需要的一切。 list_box 所占用的块实际上是它的更改方法。 你告诉它当它发生变化时要做什么。 当您获得所需的值时,只需推迟程序的其余部分运行即可。
这是所有 GUI 应用程序背后的基本思想:构建初始应用程序,然后等待“事件”,这将为您创建响应的新状态。 例如,在 ruby-gnome2 中,您可以使用带有 Gtk::ComboBox 会改变你的应用程序的状态。 像这样的事情:
即使在工具包之外,您也可以使用 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.
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:
Even outside of a toolkit you can get a "free" events system using Ruby's Observer module. Hope this helped.