BAHASA PEMROGRAMAN RUBY DI LINUX MENGGUNAKAN RUBY-GTk
Oleh:Slamet Nurhadi
DEPARTEMEN TEKNIK INFORMATIKA UNIVERSITAS NASIONAL DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
Menginstall Ruby dengan menggunakan kode dibawah ini ketik di console $> sudo apt-get install ruby-full libruby libruby-extras ruby1.8-dev
Untuk menginstall paket-paket Ruby-GNOME2 di Ubuntu Linux cukup dengan mengetikan perintah: $> sudo apt-get install ruby-gnome2
#!/usr/bin/env ruby require 'gtk2' window = Gtk::Window.new window.border_width = 10 window.set_size_request(150, 1) window.title = "Vd. Boxes" window.signal_connect('delete_event') { false } window.signal_connect('destroy') { Gtk.main_quit } labels = %w[Andrew Joe Samantha Jonatan] # homogeneous, spacing vbox = Gtk::VBox.new(false, 0) labels.each do |name| btt = Gtk::Button.new(name) btt.signal_connect("clicked") {btt.destroy} DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
vbox.pack_start_defaults(btt) end window.add(vbox) window.show_all Gtk.main gambar tampak seperti ini:
#!/usr/bin/env ruby require 'gtk2' window = Gtk::Window.new window.border_width = 20 window.set_size_request(150, 1) window.title = "HBoxes" window.signal_connect('delete_event') { false } window.signal_connect('destroy') { Gtk.main_quit } labels = %w[Jakarta Pekalongan Semarang Surabaya]
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
# homogeneous, spacing hbox = Gtk::HBox.new(false, 0) labels.each do |name| btt = Gtk::Button.new(name) btt.signal_connect("clicked") {btt.destroy} # child, expand, fill, padding hbox.pack_start(btt, false, false, 5) end window.add(hbox) window.show_all Gtk.main
#!/usr/bin/env ruby require 'gtk2' window = Gtk::Window.new(Gtk::Window::TOPLEVEL) window.set_title "Panes" window.border_width = 10 window.set_size_request(150, 150) window.signal_connect('delete_event') { Gtk.main_quit } button1 = Gtk::Button.new("Resize") button2 = Gtk::Button.new("Aku") button1.signal_connect( "clicked" ) { window.destroy } button2.signal_connect( "clicked" ) { window.destroy } DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
paned = Gtk::VPaned.new paned.add1(button1) paned.add2(button2) window.add(paned) window.show_all Gtk.main akan menampilkan seperti ini
#!/usr/bin/env ruby require 'gtk2' window = Gtk::Window.new window.border_width = 10 window.set_size_request(200, 1) window.title = "Expanders" window.signal_connect('delete_event') { false } window.signal_connect('destroy') { Gtk.main_quit } fixed = Gtk::Fixed.new expander = Gtk::Expander.new("klick disini") label = Gtk::Label.new("Tampilkan atau sembunyikan ,\nini adalah pilihan anda") expander.add(label)
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
# make the child (label) visible expander.expanded = true window.add(expander) window.show_all Gtk.main
#!/usr/bin/env ruby require 'gtk2' def another_tab; puts "Switching"; end def report_press(w); puts "Somebody pressed gumbek (button) w=#{w}"; end window = Gtk::Window.new(Gtk::Window::TOPLEVEL) window.set_title "Notebook with 3 tabs" window.border_width = 10 window.set_size_request(300, 1) # The delete_event is only needed if you plan to # intercept the destroy / quit with a dialog box. # # window.signal_connect('delete_event') { false } # Normally you'd catch the destroy signal, however # as the following code shows deleteevent can do # anything you want also quit for that matter: # window.signal_connect('destroy') { Gtk.main_quit } window.signal_connect('delete_event') { Gtk.main_quit } nb = Gtk::Notebook.new label1 = Gtk::Label.new("1st Pg") label2 = Gtk::Label.new("2st Pg") label3 = Gtk::Label.new("3rd Pg") note1 = Gtk::Label.new("Page 1.\nCan switch to ...") note2 = Gtk::Label.new("2 page.\nSwitch to ...") button = Gtk::Button.new("Gumbek") DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
# In Ruby Notebook works without {{ signal_connect }}. # # In Ruby the following doesn't work (Label does not respond to 'clicked' # note1.signal_connect('clicked') { another_tab } # note2.signal_connect('clicked') { another_tab } # However you can place a button into a notebook and klick away. button.signal_connect( "clicked" ) {|w| report_press(w) } nb.signal_connect('changecurrentpage') { another_tab } nb.append_page(note1, label1) nb.append_page(note2, label2) nb.append_page(button, label3) window.add(nb) window.show_all Gtk.main
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
CATATAN PENTING: Instalasi Ruby dan Ruby/GTK Mencoba Bahasa Pemrograman Ruby di Linux menggunakan Ubuntu. Ruby/GTK in. Jangan lupa untuk menginstall bahasa pemrograman Ruby terlebih dahulu: $> sudo apt-get install ruby-full libruby libruby-extras ruby1.8-dev
Untuk menginstall paket-paket Ruby-GNOME2 di Ubuntu Linux cukup dengan mengetikan perintah: $> sudo apt-get install ruby-gnome2
DEPARTEMEN TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL