穿鞋上路

Shoes是一个用于Ruby的简单而强大的GUI库,它使用Block和Closure的方式编写程序,使得代码非常清晰简洁。本文介绍了如何安装Shoes并展示了Hello World及一个计算器的例子,演示了Shoes的易用性和跨平台特性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

[url=http://code.whytheluckystiff.net/shoes/]Shoes[/url]是Ruby的一个简单的桌面程序库

下载Shoes,将shoes.exe加入path
看看Hello World例子hello.rb:
[code]
Shoes.app do
button "Press Me" do
alert "Hello Shoes!"
end
end
[/code]
运行shoes hello.rb即可

再看看自带的计算器的例子:
[code]
class Calc
def initialize
@number = 0
@previous = nil
@op = nil
end

def to_s
@number.to_s
end

(0..9).each do |n|
define_method "press_#{n}" do
@number = @number.to_i * 10 + n
end
end

def press_clear
@number = 0
end

{'add' => '+', 'sub' => '-', 'times' => '*', 'div' => '/'}.each do |meth, op|
define_method "press_#{meth}" do
if @op
press_equals
end
@op = op
@previous, @number = @number, nil
end
end

def press_equals
@number = @previous.send(@op, @number.to_i)
@op = nil
end

end

number_field = nil
number = Calc.new
Shoes.app :height => 250, :width => 200, :resizable => false do
background "#EEC".."#996", :radius => 5,
:top => 2, :left => 2, :width => -4, :height => -4

stack :margin => 4 do

stack :margin => 8 do
number_field = text "<b>#{number}</b>"
end

flow :width => 218, :margin => 4 do
%w(7 8 9 / 4 5 6 * 1 2 3 - 0 Clr = +).each do |btn|
button btn, :width => 46, :height => 46 do
method = case btn
when /[0-9]/: 'press_'+btn
when 'Clr': 'press_clear'
when '=': 'press_equals'
when '+': 'press_add'
when '-': 'press_sub'
when '*': 'press_times'
when '/': 'press_div'
end

number.send(method)
number_field.replace "<b>#{number} </b>"
end
end
end
end

end
[/code]
用Block、Closure的方式来开发桌面程序,代码非常的清晰简洁

Shoes目前支持OS X、XP、Vista、Debian、Ubuntu,跨平台做的也非常不错
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值