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 L.
BUG: crashes on dave winer’s feed Scripting News -
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((“Google”))
do |f|
if f.status.include? “200”
begin
matches = MATCH_EXP.match(f.read)
return result_format(matches[2])
rescue NoMethodError
return “==> Expression not understood.”
rescue Exception
return “==> Expression not understood. (#{$!.class.inspect},
#{$!.inspect})”
end
else
return “==> Response error: #{f.status.inspect}”
end
end
end
private
MATCH_EXP = Regexp.new(“
(.?) =
(.?)”)
def self.result_format(s)
s.gsub(“
”,“,”).gsub(“×”,“x”).gsub(“”,“^”).gsub(“”, “”)
end
end
class MainForm
attr_accessor :form
def initialize
form = Form.new
form.FormBorderStyle = FormBorderStyle::Sizable
form.SizeGripStyle = SizeGripStyle::Show
form.StartPosition = FormStartPosition::CenterScreen
form.Text = “Google Calculator”
form.Size = Size.new(220, 200)
expressionGroupBox = GroupBox.new
expressionGroupBox.Dock = DockStyle::Top
expressionGroupBox.Width = 215
expressionGroupBox.Height = 50
expressionGroupBox.Text = "Expression"
expressionTextBox = TextBox.new
expressionTextBox.Location = Point.new(5, 20)
expressionTextBox.Size = Size.new(125, 21)
expressionTextBox.Anchor = AnchorStyles::Left | AnchorStyles::Top |
AnchorStyles::Right
calcButton = Button.new
calcButton.Size = Size.new(75, 23)
calcButton.Location = Point.new(135, 19)
calcButton.Text = "Calculate"
calcButton.Anchor = AnchorStyles::Right | AnchorStyles::Top
calcButton.Enabled = false
expressionGroupBox.Controls.Add(expressionTextBox)
expressionGroupBox.Controls.Add(calcButton)
resultGroupBox = GroupBox.new
resultGroupBox.Dock = DockStyle::Fill
resultGroupBox.Text = "Results"
resultGroupBox.Size = Size.new(100, 100)
resultTextBox = TextBox.new
resultTextBox.Location = Point.new(5, 20)
resultTextBox.Size = Size.new(90, 75)
resultTextBox.Anchor = AnchorStyles::Right | AnchorStyles::Top |
AnchorStyles::Left | AnchorStyles::Bottom
resultTextBox.Multiline = true
resultTextBox.ReadOnly = true
# resultTextBox.BorderStyle = BorderStyle::None
#resultTextBox.ForeColor = Color.Black
#resultTextBox.BackColor = Color.White
expressionTextBox.TextChanged do |sender, args|
calcButton.Enabled = (expressionTextBox.Text.strip != "")
end
calcButton.Click do |sender, args|
begin
calcButton.Text = "Working ..."
calcButton.Enabled = false
resultTextBox.Text = GoogleCalc.calc(expressionTextBox.Text)
rescue Exception
resultTextBox.Text = "==> Error occurrred: $!.message"
ensure
calcButton.Text = "Calculate"
calcButton.Enabled = true
end
end
resultGroupBox.Controls.Add(resultTextBox)
form.Controls.Add(resultGroupBox)
form.Controls.Add(expressionGroupBox)
form.PerformLayout
@form = form
end
end
Application.EnableVisualStyles
Application.SetCompatibleTextRenderingDefault false
Application.Run(MainForm.new.form)