I have a Ruby program and want to be able to pick a directory using a
dialog box. Does anyone have a quick, easy piece of code that will
bring up a dialog box to pick a directory? That’s the only GUI part of
the program I need. I don’t want to learn a whole GUI programming
library just to create a directory picking dialog box, so if someone has
some code I can just plug into my existing Ruby program I would greatly
appreciate it.
I have a Ruby program and want to be able to pick a directory using a
dialog box. Does anyone have a quick, easy piece of code that will
bring up a dialog box to pick a directory? That’s the only GUI part of
the program I need. I don’t want to learn a whole GUI programming
library just to create a directory picking dialog box,
But you’re going to have to. The only way to do this is through a GUI
library.
so if someone has
some code I can just plug into my existing Ruby program I would greatly
appreciate it.
It really depends on which OS you are working and which library you
want to use. You can use ffi gem to open such dialog directly from
shared library or you can use some Ruby GUI bindings like RubyTk,
WxRuby or QtRuby. So there is no general solution and depends much of
your environment and your decision which library you will use.
I figured out how to do it easily using FXRuby. The code snippet below
worked just fine and did exactly what I wanted. Not sure it’s the most
efficient way, but it worked.
From: Albert S. [email protected]
Subject: Re: Need Code to Create Directory Picking Dialog Box
Date: Thu, 11 Feb 2010 05:41:38 +0900
Message-ID: [email protected]
Alex DeCaria wrote:
I have a Ruby program and want to be able to pick a directory using a
dialog box. Does anyone have a quick, easy piece of code that will
And here’s how to do it in Ruby/Tk:
And, if you hate that the eventloop wastes CPU power,
require ‘tk’
Tk.root.withdraw
def choose_directory(keys = {})
th = Thread.new{Tk.mainloop} # start the eventloop
begin
dir = Tk.chooseDirectory(keys) # show dialog
Tk.update # for safety
ensure
th.kill # stop the eventloop
end
dir
end
if FILE == $0
dir = choose_directory(:initialdir => ‘/’, :title => ‘test test
test’)
p dir
p choose_directory(:initialdir => dir, :title => ‘test TEST test’)
end