Anda di halaman 1dari 21

TUTORIAL RUBY

oleh:
Slamet nurhadi

TEKNIK INFORMATIKA
UNIVERSITAS NASIONAL
Install GUI Toolkit Shoes dan dapat di download di www.shoesrb.com

coba kita ketik kode sebagai berikut dan simpan dengan file tombol1.rb

Shoes.app do
button("Press me") do
alert Time.now
end
end

dan buka file dengan shoes toolkit

dan hasilnya akan sebagai berikut


buat file tombol2.rb lalu salinlah kode dibawah ini

Shoes.app(:width => 400, :height => 180) do


button("Get the time", :top => 100, :left => 20) do
alert Time.now
end
button("Get a random number", :top => 100, :left => 150) do
alert rand(100)
end
end

lalu hasilnya akan nampak seperti dibawah ini


buat file tombol3.rb lalu salinlah kode dibawah ini

Shoes.app(:width => 300, :height => 300) do


flow :width => 280 do
%w{7 8 9 / 4 5 6 * 1 2 3 + 0 C = -}.each do |key|
button key, :width => 70, :height => 70 do
alert "You pressed #{key}"
end
end
end
end

maka hasinya seperti dibawah ini


buat file tombol4.rb lalu salinlah kode dibawah ini

Shoes.app(:width => 300, :height => 300) do


flow :width => 0.9, :height => 0.8, :margin => 5 do
%w{7 8 9 / 4 5 6 * 1 2 3 + 0 C = -}.each do |key|
button key, :width => 0.22, :height => 0.22 do
alert "You pressed #{key}"
end
end
end
end
Shoes.app(:width => 300, :height => 300) do
stack :width => 0.9, :height => 0.9 do
%w{7 8 9 / 4 5 6 * 1 2 3 + 0 C = -}.each do |key|
button key, :width => 0.22, :height => 30 do
alert "You pressed #{key}"
end
end
end
end

buat file shape.rb lalu salinlah kode dibawah ini

Shoes.app do
oval :left => 10, :top => 10, :radius => 50
end

maka hasilnya akan sebagai berikut


buat file shape color.rb lalu salinlah kode dibawah ini

Shoes.app do
oval :left => 10, :top => 10, :radius => 50, :fill => red
end
buat file shape color2.rb lalu salinlah kode dibawah ini

Shoes.app do
fill red
stroke blue
oval :left => 10, :top => 10, :radius => 50
oval :left => 110, :top => 70, :radius => 20
fill black
stroke black
oval :left => 160, :top => 80, :radius => 10
end
buat file draw.rb lalu salinlah kode dibawah ini

Shoes.app do
# Draw a line from 100, 150 to 250, 200
line 100, 150, 250, 200

# Draw a box at 50, 50 of width 150, height 75


rect 50, 50, 150, 75

# Draw a box at 210, 75 of width 250, height 100 and curved corners (radius 5)
rect 210, 75, 250, 100, 5

# Draw an arrow 20 pixels wide at 50, 50 with red fill


fill red
arrow 50, 50, 20
end
buat file text.rb lalu salinlah kode dibawah ini

Shoes.app do
stack :width => 150, :height => 1.0 do
para "This is some text."
para "I am Slamet nurhadi"
para "I Studied of Information Engineering"
para "How about you ?"
para "Hello, world!"
end
end

buat file text2.rb lalu salinlah kode dibawah ini


Shoes.app do
stack do
title "My App!"
subtitle "It's a great app.."
para "Hello! This is a ", em("really"), " great app!"
para "How about some ", strong("bold"), " text?"
end
end
buat file text3.rb lalu salinlah kode dibawah ini

Shoes.app do
stack do
para "Example 1", :font => "Helvetica bold 24px"
para "Example 2", :font => "Times"
para "Example 3", :weight => "bold", :size => "12pt"
end
end
buat file element.rb lalu salinlah kode dibawah ini

Shoes.app do
stack do
para "Please click ", link("here!").click { alert "You clicked me!" }
end
end

buat file editbox.rb lalu salinlah kode dibawah ini

Shoes.app do
stack do
edit_line
edit_box :width => 100, height => 100
end
end
buat file button.rb lalu salinlah kode dibawah ini
Shoes.app do
@button = button("Press me")
@button.click { @button.move(rand, rand) }
end

tombol akan bergerak pindah jika tombol di klik


buat file element2.rb lalu salinlah kode dibawah ini

Shoes.app :height => 500, :width => 500 do


stack do
para "Type:"
@e = edit_box :width => 0.9, :height => 300
@chars = para "0 characters"
@e.change do
@chars.text = "#{@e.text.length} characters"
end
end
end

dapat menghitung jumlah karakter yang ditulis


buat file event.rb lalu salinlah kode dibawah ini

Shoes.app do
@shape = rect 10, 10, 50, 50, :fill => red, :border => 0
motion do |x, y|
@shape.move x - 25, y - 25
end
end

Pointer Mouse akan diikuti shape


buat file event2.rb lalu salinlah kode dibawah ini
Shoes.app do
@t = para "Hello, world!"

if confirm("Change text?")
@t.text = "Aw, shucks.."
end
end

Jika menekan tombol cancel maka akan tampil tulisan “Hello, World!”
buat file event3.rb lalu salinlah kode dibawah ini

Shoes.app do
@t = para "Hello, world!"
button "Change text" do
@t.text = ask("Change to what?")
end
end

mengubah tulisan text


menjadi seperti yang dituliskan “Assalaamualaikum”
buat file color2.rb lalu salinlah kode dibawah ini

Shoes.app do
fill red
@s = rect 100, 100, 50, 50
button "Change color of square" do
@s.fill = ask_color("Pick a color")
end
end

mengubah warna shape dapat dilihat gambar di bawah ini


buat file texteditor.rb lalu salinlah kode dibawah ini

Shoes.app(:width => 600, :height => 500) do


stack do
flow do
@open_button = button "Open"
@save_button = button "Save"
end
@t = edit_box :width => 0.9, :height => 400
end
@open_button.click do
@t.text = File.read(ask_open_file)
end
@save_button.click do
File.new(ask_save_file, 'w') do |f|
f.puts @t.text
end
end
end

Anda mungkin juga menyukai