# How to make a Dialog as a function?

**URL:** <https://www.ruby-forum.com/t/how-to-make-a-dialog-as-a-function/159930>\
**Category:** wxRuby\
**Created:** [February 10, 2009, 4:14am UTC](https://www.ruby-forum.com/t/how-to-make-a-dialog-as-a-function/159930 "2009-02-10T04:14:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Carlos\_Troncoso\_Phi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3d9bf3/32.png) [@Carlos\_Troncoso\_Phi](https://www.ruby-forum.com/u/Carlos_Troncoso_Phi)\
**Post date:** [February 10, 2009, 4:14am UTC](https://www.ruby-forum.com/t/how-to-make-a-dialog-as-a-function/159930/1 "2009-02-10T04:14:00Z")

</div>

Hi there!  
Let me state my dilema. I’m most sure that there must be working codes  
that does what I want.  
Usually, a function is just a “black box” that you feed “something”,  
then it “does something to it”, and it throws out “something else”.  
i.e. hometown = find\_hometown(employee)

Well, I’d like to have a dialog where you can choose from a list, click  
ok and the dialog would return an answer  
i.e. hometown = mydlg.find\_hometown(employee\_array)

My logics dilema is that, find\_hometown must do the following:

- Instance the dialog
- Fill a Wx::Choice with employee\_array
- show modal
- Let the user choose and click OK
- Do some SQL magic to retrieve the hometown of the selected item
- Return the value
- Destroy the dialog.  
Everything is ok with this procedure, except that:  
As in ruby, the last eval is what is returned, how can I self.distroy  
the dialog if the return value must be the LAST line of code in the  
function?

I’ve noticed that MessageDialog displays such effect, but, I’d really  
like to know the logic behind it.

Thx everyone!

–  
Carlos Troncoso Phillips

---

<div class="post-metadata">

**Author:** ![Carlos\_Troncoso\_Phi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/3d9bf3/32.png) [@Carlos\_Troncoso\_Phi](https://www.ruby-forum.com/u/Carlos_Troncoso_Phi)\
**Post date:** [February 11, 2009, 7:49am UTC](https://www.ruby-forum.com/t/how-to-make-a-dialog-as-a-function/159930/2 "2009-02-11T07:49:18Z")

</div>

Hi

Carlos Troncoso Phillips wrote:

> Well, I’d like to have a dialog where you can choose from a list, click  
> ok and the dialog would return an answer  
> i.e. hometown = mydlg.find\_hometown(employee\_array)

The class to start with is Wx::SingleChoiceDialog. Your message made me  
look at this class for the first time and it needs a little developer  
attention, but it should work fine for your purposes.

With dialogs the primary return value is whether or not the user chose  
to proceed - ie did they click ‘OK’ or ‘Cancel’ (or shut the dialog, or  
press )? In deciding what to do next, an application should test  
this before it thinks about acting upon what value was chosen.

Conventionally, wxRuby dialogs signal this by returning an integer  
constant from show\_modal. Your code should test this value, then proceed  
accordingly.

> As in ruby, the last eval is what is returned, how can I self.distroy  
> the dialog if the return value must be the LAST line of code in the  
> function?

See the example code below as a starting point. It shows how, if you  
want, you can provide a class method as a quick way to select something  
from a list, or return ‘nil’ if the user cancelled the action.

If you create a dialog with a parent window, then show it temporarily,  
you don’t need to call #destroy on it. User code rarely need to call  
#destroy (see the docs); in most normal cases wxRuby handles window  
deletion and clean up automatically.

alex  
\_\_  
require ‘wx’

class EmployeeDialog \< Wx::SingleChoiceDialog  
EMPLOYEES = { ‘Fred’ =\> ‘London’,  
‘Brenda’ =\> ‘Manchester’,  
‘Billy’ =\> ‘Glasgow’ }

def self.find\_hometown(parent)  
dlg = new(parent, ‘Select employee’, ‘Choose one’, EMPLOYEES.keys)  
if dlg.show\_modal == Wx::ID\_OK  
return dlg.hometown  
else  
return nil  
end  
end

def hometown  
EMPLOYEES[string\_selection]  
end  
end

Wx::App.run do  
frame = Wx::Frame.new nil, :title =\> ‘Employeees’  
button = Wx::Button.new frame, :label =\> ‘Select employee’  
evt\_button(button) { puts EmployeeDialog.find\_hometown(frame) }  
frame.show  
end
