From: Justin Bailey Date: 2006-02-22T09:23:21+09:00 Subject: Re: ANN: Second drop of RubyCLR bridge ------=_Part_24443_11281700.1140567371701 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Here's some more fun - an alternate way to initialize properties. Now if only I could get rid of the argument to the block: # begin code Form.class_eval do def initialize(*args, &blk) super unless blk.nil? blk.call(self) end end end class MainForm attr_accessor :form def initialize @form =3D Form.new do |f| f.FormBorderStyle =3D FormBorderStyle::Sizable f.SizeGripStyle =3D SizeGripStyle::Show f.StartPosition =3D FormStartPosition::CenterScreen f.Text =3D "Initialize From Block!" f.Size =3D Size.new(220, 200) end end end Application.Run(MainForm.new.form) On 2/21/06, m4dc4p wrote: > > This is a great step forward. I've done a lot of Windows Forms work and > ..NET has a really good UI layer for working with. What's more, it has > *fantastic* documentation. I tried to learn Tcl/Tk but the challenge of > translating from Perl and learning a new tool kit at the same time was > too much. This is just what I wanted to write Ruby GUI apps. > > Anyways, I banged together a little 'Google Calculator' program that > uses Google's calcuator service for evaluating expressions. Sounds > trivial until you need to divide miles by fortnights or similar (try > it!). > > Heres the code - just copy it into the same directory with the > rubyclr.rb file and the RbDynamicMethod.dll: > > > # RSS Reader sample application > # (C)Copyright 2006 John Lam > > # BUG: crashes on dave winer's feed http://www.scripting.com/rss.xml - > bug in rss feed reader > > require 'rubyclr' > > RubyClr::reference 'System' > RubyClr::reference 'System.Drawing' > RubyClr::reference 'System.Windows.Forms' > > include System::Drawing > include System::Drawing::Drawing2D > include System::Windows::Forms > > > require 'open-uri' > require 'cgi' > > class GoogleCalc > > def self.calc(expr) > open(("http://www.google.com/search?q=3D#{CGI.escape(expr.strip)}")) > do |f| > if f.status.include? "200" > begin > matches =3D MATCH_EXP.match(f.read) > return result_format(matches[2]) > rescue NoMethodError > return "=3D=3D> Expression not understood." > rescue Exception > return "=3D=3D> Expression not understood. (#{$!.class.inspect}, > #{$!.inspect})" > end > else > return "=3D=3D> Response error: #{f.status.inspect}" > end > end > end > > private > > MATCH_EXP =3D Regexp.new(" src=3D/images/calc_img.gif> (.*?) =3D > (.*?)") > > def self.result_format(s) > s.gsub(" > ",",").gsub("×","x").gsub("","^").gsub("", "") > end > > > end > > class MainForm > attr_accessor :form > > def initialize > form =3D Form.new > form.FormBorderStyle =3D FormBorderStyle::Sizable > form.SizeGripStyle =3D SizeGripStyle::Show > form.StartPosition =3D FormStartPosition::CenterScreen > form.Text =3D "Google Calculator" > form.Size =3D Size.new(220, 200) > > expressionGroupBox =3D GroupBox.new > expressionGroupBox.Dock =3D DockStyle::Top > expressionGroupBox.Width =3D 215 > expressionGroupBox.Height =3D 50 > expressionGroupBox.Text =3D "Expression" > > expressionTextBox =3D TextBox.new > expressionTextBox.Location =3D Point.new(5, 20) > expressionTextBox.Size =3D Size.new(125, 21) > expressionTextBox.Anchor =3D AnchorStyles::Left | AnchorStyles::Top | > AnchorStyles::Right > > calcButton =3D Button.new > calcButton.Size =3D Size.new(75, 23) > calcButton.Location =3D Point.new(135, 19) > calcButton.Text =3D "Calculate" > calcButton.Anchor =3D AnchorStyles::Right | AnchorStyles::Top > calcButton.Enabled =3D false > > expressionGroupBox.Controls.Add(expressionTextBox) > expressionGroupBox.Controls.Add(calcButton) > > resultGroupBox =3D GroupBox.new > resultGroupBox.Dock =3D DockStyle::Fill > resultGroupBox.Text =3D "Results" > resultGroupBox.Size =3D Size.new(100, 100) > > resultTextBox =3D TextBox.new > resultTextBox.Location =3D Point.new(5, 20) > resultTextBox.Size =3D Size.new(90, 75) > resultTextBox.Anchor =3D AnchorStyles::Right | AnchorStyles::Top | > AnchorStyles::Left | AnchorStyles::Bottom > resultTextBox.Multiline =3D true > resultTextBox.ReadOnly =3D true > # resultTextBox.BorderStyle =3D BorderStyle::None > #resultTextBox.ForeColor =3D Color.Black > #resultTextBox.BackColor =3D Color.White > > expressionTextBox.TextChanged do |sender, args| > calcButton.Enabled =3D (expressionTextBox.Text.strip !=3D "") > end > > calcButton.Click do |sender, args| > begin > calcButton.Text =3D "Working ..." > calcButton.Enabled =3D false > resultTextBox.Text =3D GoogleCalc.calc(expressionTextBox.Text) > rescue Exception > resultTextBox.Text =3D "=3D=3D> Error occurrred: $!.message" > ensure > calcButton.Text =3D "Calculate" > calcButton.Enabled =3D true > end > end > > resultGroupBox.Controls.Add(resultTextBox) > > form.Controls.Add(resultGroupBox) > form.Controls.Add(expressionGroupBox) > form.PerformLayout > @form =3D form > end > end > > Application.EnableVisualStyles > Application.SetCompatibleTextRenderingDefault false > Application.Run(MainForm.new.form) > > > > John Lam wrote: > > This is a much more complete drop of the bridge: > > http://www.iunknown.com/articles/2006/02/20/second-drop-of-rubyclr > > > > To show off its features, I've built a non-trivial all-Ruby Windows > Forms > > 2.0 RSS Reader application that's in the distribution. It's been tested > with > > the RTM bits of .NET FX 2.0, and Ruby 1.8.2. > > > > Comments & questions more than welcome! > > > > Cheers, > > -John > > http://www.iunknown.com > > > > ------=3D_Part_9676_7336074.1140486137995-- > > > ------=_Part_24443_11281700.1140567371701--