如何在Shoes GUI中禁止输入任何其他没有数字的字符?
Shoes.app(resizable: false) do
background white..blue
@t = 0
tab = []
0.upto(9) {|i| tab.push(String(i))}
@b = edit_line(left: 120, top: 10) do
if @b.text != "" and tab.include?(@b.text)
keypress do |k|
if k == "\n"
@av.text = @av.text.to_i + @b.text.to_i
@b.text = ""
@t = @t + 1
@show.text = "Average: " + (@av.text.to_i / @t).to_s
end
end
end
end
@d = flow(left: 325, top: 10) do
@av = para strong("Average")
@show = para strong("Show!")
end
end
我试图仅在“tab”包含我们在“edit_line”中输入的任何字符时才允许添加数字和计算平均值,但是当我添加这个时,程序完全停止了正确的工作:
and tab.include?(@b.text)
答案 0 :(得分:0)
您可以使用=〜运算符来测试正则表达式的字符串:
def valid?(text)
text =~ /^[0-9]+/ ? true : false
end
用法:
if valid?("ssd11") # return false
#do something