First Ruby/Gtk Scripts

The simplest Ruby/Gtk scripts tend to look quite a bit like their Ruby/Tk counterparts. You require the toolkit, then define the windows, widgets, and functions that make up the logic of your program, and finally invoke an event loop. A minimal Ruby/Gtk application is shown in Listing 18.1.

Listing 18.1. minimal_1.rb
01:   #!/usr/bin/env ruby
02:
03:   require 'gtk'
04:
05:   win = Gtk::Window.new
06:   win.set_title('First Ruby/Gtk application')
07:   win.signal_connect('destroy') {exit}
08:   win.show_all
09:
10:   Gtk::main
11:   puts 'All done.'

When you run minimal_1.rb, an empty window appears. You can close the window by clicking the closer widget (depending on the window manager in use, typically a box, marked with an X, in ...

Get Sams Teach Yourself Ruby in 21 Days now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.