从 http://github.com/rickyzheng/GtkSimpleLayout/tree/master下载了最新的代码,编译的时候却无法通过,显示SyntaxError。
引用
c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `gem_original_require': ./simple_layout.rb:105: syntax error, unexpected ',', expecting '|' (SyntaxError)
define_method(k) do |*args, &block|
^
./simple_layout.rb:487: syntax error, unexpected kEND, expecting $end from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from test1.rb:2
define_method(k) do |*args, &block|
^
./simple_layout.rb:487: syntax error, unexpected kEND, expecting $end from c:/ruby/lib/ruby/site_ruby/1.8/rubygems/custom_require.rb:31:in `require'
from test1.rb:2
调查了之后发现,原来Ruby 1.8中,define_method中不能使用block,Ruby 1.9中没有问题。因此对simple_layout.rb的代码进行了如下修改。
105行: define_method(k) do |*args, &block| create_component(v, args, block) end ↓ eval <<-EOM def #{k}(*args, &block) create_component("#{v}", args, block) end EOM 432行: def create_component(component_class, args, block) @common_attribute ||= [] options = {} options = args.pop if args.last.is_a?(Hash) options.merge! @common_attribute.last if @common_attribute.last + w = eval("#{component_class}.new(*args)") layout_component(w, options, &block) end
终于编译通过了。

简单的实验
竖排三个按钮
require 'gtk2' require 'simple_layout' class MyWin < Gtk::Window include SimpleLayout::Base def initialize super add my_layout signal_connect('destroy') do Gtk.main_quit end end def my_layout vbox do button 'Mice' button 'Eagles' button 'Quail' end end end MyWin.new.show_all Gtk.main
计算器布局
require 'gtk2' require 'simple_layout' class MyWin < Gtk::Window include SimpleLayout::Base def initialize super add my_layout signal_connect('destroy') do Gtk.main_quit end end def my_layout vbox do with_attr :border_width => 3 do hbox do entry :id => :ent_input, :layout => [true, true, 5] end hbox do frame do label 'M', :set_size_request => [20, 20] end hbutton_box do button 'Backspace' button 'CE' button 'C' end end hbox do vbutton_box do button 'MC' button 'MR' button 'MS' button 'M+' end with_attr :layout => [true, true] do number_and_operators_layout end end end end end def number_and_operators_layout vbox do [ ['7', '8', '9', '/', 'sqt'], ['4', '5', '6', '*', '%'], ['1', '2', '3', '-', '1/x'], ['0', '+/=', '.', '+', '=']].each do |cols| hbox :layout => [true, true] do cols.each do |txt| button txt, :set_size_request => [20, 20], :layout => [true, true] end end end end end end MyWin.new.show_all Gtk.main
模仿 http://the-shoebox.org/apps/126写的小程序
require 'gtk2' require 'simple_layout' class MyWin < Gtk::Window include SimpleLayout::Base def initialize super('Little Helper v0.4') add my_layout signal_connect('destroy') do Gtk.main_quit end register_auto_events() end def my_layout entry1 = nil vpaned do area_first do frame 'Awful numbers', :border_width => 5 do vbox do label 'Hours/week', :set_size_request => [200, -1] @entry1 = entry :layout => [false, false], :max_length => 20, :id => :entry1 label 'Money/hour', :set_size_request => [200, -1] @entry2 = entry :layout => [false, false], :max_length => 20, :text => '16', :id => :entry2 label 'Tax (in %)', :set_size_request => [200, -1] @entry3 = entry :layout => [false, false], :max_length => 20, :text => '20', :id => :entry3 end end end area_second do frame 'Rags or riches', :border_width => 5 do vbox do @lab1 = label '0.0' end end end end end def entry1_on_key_release_event(*_) @lab1.text = calculate.to_s end def entry2_on_key_release_event(*_) @lab1.text = calculate.to_s end def entry3_on_key_release_event(*_) @lab1.text = calculate.to_s end def calculate @entry1.text.to_f * @entry2.text.to_f * (([email protected]_f)/100.0) * 4.3 end end MyWin.new.show_all Gtk.main
参考:
http://coderrr.wordpress.com/2008/10/29/using-define_method-with-blocks-in-ruby-18/