Ralphs Ruby Pages

WARNING: This page contains examples MS technology being used with open source code.  If you have your open source blinders on and can not fathom why anyone uses anything other than 'free' operating systems, or think I am sleeping with the Gates devil, save yourself - leave while you still can. Consider yourself warned.

Ruby... what a gem, that's all I can say.

After 10 years with C++, VB and COM and associated MS technologies ruby is a lovely breath of fresh air to me.  Just playing with it makes me smile. Ruby is a joy to use, with a great community.  Also as it's new (in language terms anyway), it's still not quite 'set'. This means that you can help, be involved and make your own little part of it.

The aim of this page is to help create awareness of ruby, and promote ruby on Windows.

Coming soon, watch this space:   

  • Ruby on Windows tips & tricks and hints.
  • My future Ruby projects Ideas:
    • A Ruby VM (there is one but I like to play)
    • Some kind of IDE using scintilla as the editing component and the rest written in Ruby

Links

What to know more?

Ruby Code

  • Dictionary.com Lookup, uses ruby to lookup word from Dictionary.com (download)

Ruby.COM (work in progress, stand clear)
Download RubyCOM 0.0.1       RubyCOM Documentation

Here is the testbed for Ruby.COM, notice the really nice Ruby features, including dynamic evaluation being used to add methods to classes, these methods are then called from the com interface.  But it goes further than that, you can create whole new classes and invoke those through com also, right from your program! If you already know Ruby you wont be surprised at these things.  If your coming from VB & C++ you will be.

First a simple Ruby class

class Myclass 
   deftest2(x,y)
     [x,y]
   end
   
   def x=(y) 
     @x = y 
   end

   def x
     @x	
   end

   def test(x,y,z)
     "#{x} #{y} #{z}"
   end
end

 

And The VB code to use it and do all sorts of cool things.

Dim rbyObject As Object
Private Sub Form_Load()

'RubyCom Test Harness
Dim obj1 As Object
Dim Obj2 As Object
Dim WinClass As Object
Dim MyClass As Object

Set obj1 = CreateObject("Ruby.Myclass")

obj1.x = 0
While (obj1.x < 1000)
    obj1.x = obj1.x + 1
Wend

a (obj1.x = 1000)

Set Obj2 = CreateObject("Ruby.Myclass")

a (obj1.Type = Obj2.Type) ' Call inspect on each type
a (obj1.Type Is Obj2.Type) ' Compare actual objects

'Test references & finding the same object
Set WinClass = obj1.eval("WIN32OLE")
Set MyClass = obj1.eval("Myclass")

a (WinClass.Type = MyClass.Type)
a (WinClass.Type Is MyClass.Type)
a (Not (WinClass Is MyClass))
a (obj1.Type Is MyClass)
a (Not (obj1.Type Is WinClass))

'Test calling back to other com objects Using Properties
obj1.x = Form1
a (obj1.instance_eval("@x.kind_of? WIN32OLE"))
Form1.Caption = ""
obj1.instance_eval ("@x.Caption='Test'")
a (Form1.Caption = "Test")
a (WinClass.IsKind_of(obj1.eval("Class")))
a (obj1.instance_eval("@x.Caption") = "Test")

'Test returned references
obj1.x = Obj2
obj1.x.x = "Test"
a (obj1.x.x = "Test")

'Test Is and Me and nil values
obj1.instance_eval ("def makexnil! ; @x=nil ; end")
a (Not obj1.x.IsNil)
a (Not (obj1.x Is Nothing))
obj1.MakexnilMe
a (obj1.x Is Nothing)
a (Not obj1.IsNil)

'Check out [] and []=
Set obj1 = obj1.eval("{}")
obj1("1") = True
obj1("2") = True
a (obj1("1") And obj1("2"))
a (obj1("3") Is Nothing)

'Test an array
k = obj1.eval("[1,2,3,3,4,5,5,5]")
a (k(0) = 1 And k(2) = 3 And k(4) = 4)
a (UBound(k) = 7)
obj1.instance_eval ("def a_test(x); x.uniq ; end")
k = obj1.a_test(k)
a (UBound(k) = 4)
a (k(0) = 1 And k(2) = 3 And k(4) = 5)

'Test multiple args
Set obj1 = obj1.instance_eval("Myclass.new()")
a (obj1.test(1, "test", 59) = "1 test 59")

'Test Calling back from Ruby to a local object using method calls
Set rbyObject = CreateObject("Ruby.Myclass")
rbyObject.instance_eval ("def add(x); @y=0 if @y.nil? ; @x.additem('Rby ' +x + (@y+=1).to_s ); end")
rbyObject.instance_eval ("def clear(); @x.Clear ; end")
rbyObject.x = List1 'List1 is a VB listbox object
rbyObject.Clear
rbyObject.Add ("1")
rbyObject.Add ("1")
rbyObject.Add ("1")
a (List1.ListCount = 3)
rbyObject.Clear
a (List1.ListCount = 0)


'Test Method Missing ( and function name conversion )
rbyObject.instance_eval ("def method_missing(x); x.id2name ; end ")
a (rbyObject.empty = "empty")
a (rbyObject.IsEmpty = "empty?")
a (rbyObject.EmptyMe = "empty!")
a (rbyObject.NoReallyEmptyMe = "noReallyEmpty!")

a (rbyObject.xor_ = "^")
a (rbyObject.and_ = "&")
a (rbyObject.or_ = "|")
a (rbyObject.plus_ = "+")
a (rbyObject.minus_ = "-")
a (rbyObject.times_ = "*")
a (rbyObject.div_ = "/")
a (rbyObject.mod_ = "%")
a (rbyObject.greater_ = ">")
a (rbyObject.less_ = "<")
a (rbyObject.greatereql_ = ">=")
a (rbyObject.lesseql_ = "<=")
a (rbyObject.compare_ = "<=>")
a (rbyObject.insert_ = "<<")
a (rbyObject.extract_ = ">>")
a (rbyObject.match_ = "~")

rbyObject.instance_eval ("def ==(x); '==' ; end ")
rbyObject.instance_eval ("def ===(x); '===' ; end ")
rbyObject.instance_eval ("def =~(x); '=~' ; end ")

a (rbyObject.eqleql_(1) = "==")
a (rbyObject.eqleqleql_(1) = "===")
a (rbyObject.eqlmatch_(1) = "=~")


'Call renamed method on a real class
Set obj1 = obj1.eval("/regex/")
a (obj1.eqlmatch_("regex") = 0)

'Setup for the button push test
Set rbyObject = CreateObject("Ruby.Myclass")
rbyObject.x = List1
rbyObject.instance_eval ("def add(x); @y=0 if @y.nil? ; @x.additem('Rby ' +x + (@y+=1).to_s ); end")

End Sub
Sub
a(x As Boolean) If Not x Then MsgBox "Assertion Failed" Debug.Assert (False) End If End Sub
Private Sub
Command1_Click() rbyObject.Add Text1.Text End Sub
1