The article was originally published for a personal blog, but I think those who are starting to learn Ruby, or just want to write a GUI to the application will find it useful.
Entries will not. The theme today is
Shoes . Such a small kit to create a GUI for Ruby applications. For the first time I heard about
him when I tried to figure out how to do something graphic on ruby. The answer came immediately, and in two (or even three versions):
')
Looked at the first point, then the second. I learned that a lot of popular CI ++ living applications that live in today have Qt-based :) In general, I learned a lot of useful and generally positive things. I hardly looked at the shoes (I like to call them that), but in vain.
Literally a month later, I accidentally decided to try, and you know, using Shoes, creating GUI in Ruby is much easier than using non-native Qt, etc.
Actually, I think this will be the first article from the small cycle. In it, slowly but surely, I will describe the creation of unpretentious applications, any details, etc. I think it will be interesting. We will start with something trivial, so go ahead!
FTP file downloader to server

A la designing
What do we need for our project? Actually, I decided to write something simple; from the program we need the following:
- Ability to connect to the server
- Sending a file to a specified path
All this is done simply and quickly, therefore, attacks, but for a start, a small introduction.
About Shoes
Download this:
shoooes.net/downloads . Once downloaded, create desktop shortcuts for “shoes.exe” and “shoes.exe –-package”.
Articles / manuals can be viewed here:
help.shoooes.net . But in the shoes.exe itself you can open the offline manual. Reasonably convenient and detailed.
I advise you to read as necessary all from there, well, and do not forget about the fact that the site. In my opinion, there is more information there.
We write GUI
What we are going to write is the usual Ruby-code wrapped in Shoes.app do ... end block. Self inside of which is our Shoes :: App. We can call the necessary methods, use
var and other pleasures of life. I didn’t really understand how to separate the code and inherit from this class, so that more complex sentences passed under the $ shoes = self brief inside the block of shoes.
Among the many positive qualities everyone will like - cross-platform. The same ruby-code will work in different OS, only it will look a little differently:
help.shoooes.net/Introducing.htmlBut back to our task. You and I want to write a file uploader to an FTP server. To connect to the server, we will use Net :: FTP (well, just like that, from the standard package), and for the GUI, you yourself_ know what.
Getting started
Shoes.app(:title => 'FTP File Uploader', :width => 500, :height => 400, :resizable => false) do
require 'net/ftp'
background gradient rgb(255, 255, 255), rgb(150, 150, 150), :angle => 45
stack :margin => 20 do
end
end
We create our application by setting some parameters:
: title - title
: width - width
: height - height
: resizable - resize :-)
Then we connect in perspective what we need for the future to work with FTP.
After that, we make a beautiful background with a gradient at an angle (almost as in human language, to say: “set background to gradient rbg rbg with angle”, well, or something like that).
Next is the block. In it, we can set the indent from all that is around (: margin). In addition, the elements inside the stack will be located under each other, in contrast to flow.
Add items
Shoes.app(:title => 'FTP File Uploader', :width => 500, :height => 400, :resizable => false) do
require 'net/ftp'
background gradient rgb(255, 255, 255), rgb(150, 150, 150), :angle => 45
stack :margin => 20 do
caption 'FTP File Uploader'
flow :margin => 3 do
inscription 'FTP-server: '
@server = edit_line :width => 200
end
flow :margin => 3 do
inscription 'FTP-path: '
@path = edit_line :width => 200
end
flow :margin => 3 do
inscription 'Username: '
@username = edit_line :width => 200, :text => 'anonymous'
end
flow :margin => 3 do
inscription 'Password: '
@password = edit_line :width => 200, :secret => true
end
para
flow :margin => 3 do
inscription 'Filename: '
@filename = edit_line :width => 200
para ' '
button 'Browse...' do
@filename.text = ask_open_file
end
end
flow :margin => 3 do
@status = para ''
end
end
end
After we wrote the database, it's time to add a few elements, and generally add something more or less alive to the application. For this we use flow, inscription (text with a size of 10px), para (also text, only more), edit_line, button - elements of the graphical interface.
As already mentioned, flow is the same stack block, only the elements in it will follow each other.
EditLine - input field.
Inscription and para are text blocks.
Button is a banal button.
Now for something more. "@server = edit_line: width => 200" - because within the whole block Shoes.app, self is our application, then we can work with its St. and @, thus placing there elements, data, anything and working with them inside any of our constructions.
One more thing, we don’t call the buttons, because we describe their action right away. If you add a block to the button method, it will be executed when the button is pressed - conveniently, agree. Thus, when you click “Browse ...” we will have a dialog box (ask_open_file), and the name of the selected file will be recorded in our edit_line.
A little distracted. We need to look at the fruits of our creativity. To do this, run Shoes.exe and select "Open an App". Choose our .rb file and voa la :)
Final stage
Screenshot of what happened:
http://jleft.ru/wp-content/uploads/2009/05/screen11.gifSources in a separate file:
http://www.sendspace.com/file/ba7lklShoes.app(:title => 'FTP File Uploader', :width => 500, :height => 400, :resizable => false) do
require 'net/ftp'
background gradient rgb(255, 255, 255), rgb(150, 150, 150), :angle => 45
stack :margin => 20 do
caption 'FTP File Uploader'
flow :margin => 3 do
inscription 'FTP-server: '
@server = edit_line :width => 200
end
flow :margin => 3 do
inscription 'FTP-path: '
@path = edit_line :width => 200
end
flow :margin => 3 do
inscription 'Username: '
@username = edit_line :width => 200, :text => 'anonymous'
end
flow :margin => 3 do
inscription 'Password: '
@password = edit_line :width => 200, :secret => true
end
para
flow :margin => 3 do
inscription 'Filename: '
@filename = edit_line :width => 200
para ' '
button 'Browse...' do
@filename.text = ask_open_file
end
end
flow :margin => 3 do
button 'Upload' do
@status.text = ''
begin
Net::FTP.open(@server.text) do |ftp|
ftp.login(@username.text, @password.text)
ftp.chdir(@path.text)
ftp.putbinaryfile(@filename.text)
end
@status.text = 'File transfered'
rescue Exception => e
alert "Error: #{e}"
end
end
end
flow :margin => 3 do
@status = para ''
end
end
end
If something is not added / wrong - write, fix, ss. :)
UPD . Transferred to the Ruby blog