views:

1274

answers:

1

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?

+2  A: 

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.

method
Your solution does work very well.Thanks for the help!
ischnura