Topic: Uncategorized

Speaking at eRubyCon

Just a heads up to my faithful readership, I’ll be speaking about IronRuby at erubycon this August.  It’ll be my first  public speaking engagement at a technical conference, and my first real public speaking since High School (in-house presentations don’t count in my book).  I’m quite nervous but it’s a topic I’m passionate about so I’m really excited as well.  The title of the talk is "Because Iron is Battleship Gray: IronRuby In The Real World", and it won’t mention Silverlight or Rails.  Ruby is so much more than glitz and glam, and I think that tends to get lost in the hype.  She’s a sexy lady, but she’s got brains too!

Hope to see you there!

IronRuby QuickStart ReDuex

Back in January I did a post on getting started with IronRuby.  That post was based on Rev. 75 of the SVN tree.  As of  today (June 9th) the SVN tree is up to Rev. 113.  Obviously with things like RailsConf and TechEd driving a lot of the core teams work, a number of things have changed since January.  This post will basically be a rehash of the previous one, but updated for all the new quirks that have been introduced.

Getting The Source

As before, I recommend using TortoiseSVN to grab the source. Downloading will take a bit, after the 3.5MB of source is downloaded you should end up with a directory structure like the following:

image

Compiling IronRuby

Inside of the trunk directory you will find the IronRuby.sln file, double-clicking it will open the solution (note that the solution file is now in VS2008 format instead of the previous VS2005 format).  You may get a warning, you can select “Load the Project Normally” and uncheck “Ask me this for all projects in the solution.”

Once the solution is loaded, keep the solution on the “Debug” configuration.  Do NOT select “ExternalDebug” (you’ll get broken references if you do):

image

Next we will manually remove the “SIGNED” compilation symbol from all the projects, as of Rev. 113 you have to manually remove it from the IronRuby.Libraries, Microsoft.Scripting, and Microsoft.Scripting.Core (be sure not to delete the DLR symbol) projects.

image

Once those are removed you can build the source!

Running That Which You Have Wrought

Where has previously our compilation would output into a bin\Debug folder, the resulting files are now found in \trunk\build\debug, which should look like this:

image

Due to a conflict with rubinius, the rbx executable is now ir.

As before you can just run ir.exe and enter the wonderful world of ruby, or you can continue on to see how we roll in the .NET world.

Speaking C# With A Ruby Accent

The initial steps are basically the same as before, start a new C# console project and add references to Microsoft.Scripting.dll, Microsoft.Scripting.Core.dll, IronRuby.dll, and IronRuby.Libraries.dll.

We are going to create a simple console app that shows passing variables into and out of a ruby script.

The below program should be a good example:

 


using System;

using Ruby;

using Microsoft.Scripting.Hosting;
namespace IronRubyConsoleApp

{

    class Program

    {

        static void Main(string[] args)

        {
ScriptRuntime irruntime = IronRuby.CreateRuntime();

            ScriptEngine ir = IronRuby.GetEngine(irruntime);

            ScriptScope scope = ir.CreateScope();

            ScriptSource script = ir.CreateScriptSourceFromString("puts \"Hello, #{name}!\"\ninput + 2");

            scope.SetVariable("name", ".NET");

            scope.SetVariable("input", 2);

            int x = script.Execute<int>(scope);

            Console.WriteLine(string.Format("The Result was {0}", x));

            Console.ReadLine();

        }

    }

}

You can see a clear hierarchy here:

We have a Runtime

That has an Engine

That has a Scope

That has a Source

We create a runtime, and we get our IronRuby engine into the runtime, we create a scope and load a script using that engine.  Set a couple of variables in the scope and then execute the script within the scope that we have set the variables in.

Hopefully this is enough to get you started, you should probably check out the other CreateScriptSource methods that the engine contains, you have a veritable cornucopia of options:

image

Ruby with a .NET Accent

Another of the popular activities with IronRuby, if not the most popular, is going to be interfacing with both the .NET framework and other .NET code.  The below example shows us interfacing with System.Windows.Forms and making a simple GUI app.

 

require 'mscorlib'

require ' System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' require 'System.Drawing, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' Swf=System::Windows::Forms Sd=System::Drawing class RubyForm <Swf::Form def add_button text, location button = Swf::Button.new button.Text = text button.Location = location self.Controls.Add button button end def initialize self.Text = "RubyForm" @rbutton = add_button "Click Me!",  Sd::Point.new(150, 150) @rbutton2 = add_button "Click Me!", Sd::Point.new(150, 100) @rbutton.Click {|sender, e| Swf::MessageBox.show 'Hello World!'} @rbutton2.Click {|sender, e| Swf::MessageBox.show 'Hello, .NET!'} end end rf = RubyForm.new rf.ShowDialog

As you can see above, when requiring items from the GAC we must include the Fully Qualified Name, including version and StrongNameToken if applicable. The above code inherits from System.Windows.Forms.Form and adds a helper class for adding buttons and wires up a couple of event handlers.  Again this is just a simple example to get you started.

Summary

In this post we downloaded the IronRuby source, compiled it, and worked with IronRuby both from C# and by running a ruby script against .NET objects.  Hopefully this gets you going a little faster and onto the fun stuff quicker!

Links

IronRuby Homepage

IronRuby RubyForge Project

Ruby Language Homepage

John Lam’s homepage

How I Got Started In Software Development

Michael Eaton started it, and others took off and ran with it.  Now it’s a certified meme. So I guess I might as well join in the fray and relate the beginnings of my geekdom.

A Long, Long Time Ago…

How old were you when you started programming?

I was about 7ish when I first started. Read on for more details…

How did you you get started in programming?

When I was around 7, my uncle had an Atari 400.  I initially just played games on it, but one day I noticed a stack of Byte magazines in the corner.  Inside this magazine was source code, it said it was “BASIC” and needed to be typed in… hmm.. there’s this Atari-BASIC cartridge, and there’s a keyboard.  A light went off, I can make this machine /do/ something?  So I typed in the program, saved it to the cassette drive, and then tweaked it.

Next thing I know I’m begging my parent’s for a computer.  For my 8th birthday I got an Atari 65XE and a couple of BASIC games books, and I’ve been totally hooked ever since.  Time passed, and eventually I got into things like QBASIC, DOS Batch, and C/C++ (via an old copy of TurboC a friend loaned me).  Then in high school someone gave me a Linux CD and having a whole slew of development tools really helped.

I just kept hacking away…

What’s the most fun you’ve ever had … programming?

Staying awake for 48 hours straight and hacking an ANSI art viewer in C with Jim Balcomb.  We were probably 15 and 16 at the time and just didn’t need to rest when programming was to be had.  I think it’d kill me today.  Oh yeah, and then I delivered papers in the morning after being jacked up on coffee and Jolt and my father was convinced I was on speed…

Googoo gah gah?

What was your first language?

Well it was BASIC obviously, from Atari-BASIC, to BASICA, to QBasic, eventually to QuickBasic…

What was the first real program you wrote?

One that really stands out was a grading program I wrote in high school for the Learning department at the school.  It was all done in QBasic and had menus that were keyboard driven, grid data entry, and a printing subsystem.  I wish I still had that code…

What languages have you used since you started programming?

Define use? Here are all the languages I have written something of some complexity in:

  • AutoIT (don’t ask…)
  • Bash Shell
  • BASIC (variations there of)
  • C/C++
  • C#
  • DOS Batch
  • JavaScript
  • PHP
  • Ruby
  • TSQL (don’t ask)

..and [Insert Deity Here] knows what else…

Get a good job with more pay and you’re okay.

What was your first programming gig?

I’ve always loved programming, and have been doing it has a hobbyist since as far back as I can remember.  However, without experience (and never having finished college) I was never able to really get a foot in the door.  I worked in support at Harley-Davidson Dealer Systems for 6 years and had a chance to do some development work (I had written a number of utilities for support including SetPrinter) I jumped at the opportunity.  I actually took a “demotion” from Team Lead to Developer for the chance.

After a couple years of doing development work there I had pretty much tapped out all I could do and was stuck, that’s when I was introduced to PreEmptive Solutions and am currently doing development there.  My work at PreEmptive is much more like a “real” development job then HDDS.  At HDDS I was pretty much a cowboy left on my own.  Here I’m working with a true development team.

If you knew then what you know now, would you have started programming?

Oh hell yes, I truly believe I was born to be a computer programmer, I can imagine doing nothing else.

If there is one thing you learned along the way that you would tell new developers, what would it be?

RTFM.

Thanks Mike!

A big thank you to Mike Eaton for getting the ball rolling on this, it’s fascinating to see how my fellow tweeps and hackers got started.  I’m not going to “tag” anyone, because that’s silly, but I do encourage anyone who reads this post to do the same and link back to mike’s blog, he’s keeping a list of people responding!

Top 10 Commands In Your Shell History

Saw this post on objo’s blog, apparently it’s a meme of some kind, though his was the first I’ve seen.  But I thought it was pretty cool none the less.  Here’s mine:

$  history 1000 | awk '{a[$2]++}END{for(i in a){print a[i] " " i}}' | sort -rn
| head
114 cd
84 ls
79 irb
39 git
31 pedump
10 ps
10 objdump
10 e
8 ssh
8 dd

You can see a bit of my dual developerness there, irb and git for ruby, and pedump and e for the windows side (this is in cygwin).  What does your shell history have to say?

Cleveland Day of .NET: May 17, 2008

badgeEarlier today the website went live and registrations started. What is a “Day of .NET”? And why is it happening in Cleveland? A “Day of .NET” is, as the Day of .NET website says, “a one-day conference on all things .NET organized by developers for developers.” And that last part is why it’s happening in Cleveland. A number of us have frankly been tired of nothing happening in the Cleveland/North East Ohio area, and decided to take the bull by the horns as it were.

One of the themes we’re trying to push is that Day of .NET is .NET centric, but not .NET exclusive. So we’re hoping to bring a few speakers from other worlds (Java, Ruby, Python, etc) and learn from them. As well as explore a number of the exciting new things coming out of Microsoft these days (like the DLR, Silverlight, et. al.).

Interested in speaking? Contact speakers@clevelanddodn.org. Want to sponsor or know someone who would want to sponsor? We got lot’s of logo space to fill up, just contact sponsors@clevelanddodn.org. Most of all, get to www.clevelanddodn.org and register!

Detroit Launch Event, Post Event.

 

Tuesday (3/18) was the Microsoft Launch event in Detroit.  Being the intrepid geek I am, I decided to take a road trip and go to the event 3 hours away, rather then the one in the same town a week earlier.  Madness you say?  Nay, for I got to enjoy the good company of Corey Haines and Nate Hoellein on the way there and back as we car polled together.  While leaving at 4:30am was a bit rough, the drive was worth it once we got there.  I got to interact with a slew of smart people that I get to see far to rarely.   Dustin Campbell has a good list of everyone I ran into there (don’t worry Dustin, your secret of feeling up Jeff at lunch is safe with me).

The event du jour however was by far the Geek Dinner put on by Keith Elder, and generously sponsored by the Microsoft Visual Studio Team System..err. . Team.  If you’re ever in downtown Detroit and looking for some killer deep dish and wings, I can’t recommend PizzaPapalis enough.

After a cold beer and some hot pizza, it was time to take the long haul back to Cleveland.  Corey did a fantastic job getting us through the fog alive, and by the time I hit the bed around midnight I was more then ready for sleep. 

Welcome to the new digs!

Make yourself at home… I’m still unpacking a bit, but expect new posts very soon!

IronRuby Quick Start

IronRuby is Microsoft’s, with collaboration by the public, implementation of Ruby on their Ruby LogoDynamic Language Runtime. There’s another version of Ruby for .NET called, ironically, Ruby.NET that runs directly on the CLR. This post won’t be about that though, if you want to see a comparison, look here for a fairly good write up.

Downloading IronRuby from the SVN server and compiling in VS2005 was actually pretty painless. But after that I couldn’t find any, working, examples of getting an Ruby script running in the DLR. A big part of this is due to IronRuby still being officially “pre-Alpha” with the Scripting Host API in flux. Regardless, I hope this will be enough to get some people trying to use the latest SVN (rev. 75) up and running.

Building The Source

Download the latest revision from RubyForge using SVN, if you need a client I HIGHLY recommend TortiseSVN. Once you’re done downloading the source, you should be able to open the IronRuby.sln file in Visual Studio. We only need to make one change, and that’s to the Microsoft Scripting Project. Bring up the project properties and go to the Build tab:

signed.png

We have to remove the Conditional compilation symbol of “SIGNED”, otherwise the Scripting host will be looking for Microsoft signed copies of the IronRuby library, which we don’t have. After that go ahead and build the solution (cross your fingers if it makes you feel better).

When all is done you should end up with a bin\Debug folder in your SVN root that looks something like this:

debugdir.png

You can go ahead and start rbx from right there and begin playing with Ruby if you’d like. But if that’s all we wanted to do we would have just downloaded Ruby, right? This is IronRuby, let’s do it the .NET Way!


Hosting IronRuby in C#

Create a new Console Application solution in Visual Studio, say RubyExample. Add References to the Microsoft.Scripting.dll, IronRuby.dll, and IronRuby.Libraries.dll files.

Let’s begin with the most basic, a simple Hello World:
using System;using Ruby;using Ruby.Runtime;using Microsoft.Scripting;using Microsoft.Scripting.Hosting;namespace RubyExample{ static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { IScriptEnvironment scriptenvironment = ScriptEnvironment.GetEnvironment(); IScriptEngine rubyengine = scriptenvironment.GetEngine(“ruby”); scriptenvironment.ExecuteSourceUnit(rubyengine.CreateScriptSourceFromString(“puts’Hello World! \nPress Any Key To Continue..’”) ); Console.ReadKey(); } }
Let’s take a quick look at what we’re doing here. We’re setting up a ScriptEnvironment, this is where our Dynamic languages are going to live and play. Then out of that environment we’re asking for someone who understands Ruby. After that we’re just saying, hey ScriptEnvironment, run what the Ruby guy says.

So that’s pretty nifty, we could also tell the RubyEngine to CreateScriptSourceFromFile and move whatever code we want out of a string constant. Which is probably a good idea for anything beyond a line a two. But what if we actually want to talk back and forth? Let’s move on to the next example.

Accessing Global Variables

The easiest way to pass data between IronRuby and C# is via Global Variables. See below for an example.

As you can see I switched it up a bit and inited the Environment and Engine a bit differently, one way is more general, the other specific to IronRuby. The power of the ScriptingHost API is you can on the fly decide what language you want to use. But if you know you’re only going to be doing IronRuby, you can use the above method.

The real trick is in the second line, we get a reference to the GlobalVariables of the current IronRuby execution context. Once we have that we can start assigning global variables values and retrieving them back.

Summary

Hopefully this is enough to get you started. As I continue to delve into IronRuby, I will be sure to post what I find here, so keep an eye out!

IronRuby Homepage
IronRuby RubyForge Project
Ruby Language Homepage
DLR Hosting Spec
John Lam’s homepage

T-SQL Hex String to VarBinary (Improved)

Peter DeBetta posted a while back with a function to take a hex string and convert it to varbinary It has a couple of slight issues, the biggest of which is it can’t handle an odd number of hex digits. Below is my replacement, because it’s using bigints the upper limit isn’t as high, but it’s good enough for most things:

CREATE FUNCTION dbo.HexStrToVarBinary(@hexstr varchar(8000))
RETURNS varbinary(8000)
AS
BEGIN
    DECLARE @hex char(1), @i int, @place bigint, @a bigint
    SET @i = LEN(@hexstr) 

    set @place = convert(bigint,1)
    SET @a = convert(bigint, 0)

    WHILE (@i > 0 AND (substring(@hexstr, @i, 1) like '[0-9A-Fa-f]'))
     BEGIN
        SET @hex = SUBSTRING(@hexstr, @i, 1)
        SET @a = @a +
    convert(bigint, CASE WHEN @hex LIKE '[0-9]'
         THEN CAST(@hex as int)
         ELSE CAST(ASCII(UPPER(@hex))-55 as int) end * @place)
    set @place = @place * convert(bigint,16)
        SET @i = @i - 1

     END 

    RETURN convert(varbinary(8000),@a)
END
GO

Finally, Compression in SQL.

Here’s a good overview of the compression in SQL 2008 and why it’s a good thing.  Though I don’t quite get the point on memory, don’t you have to decompress the data at some point?  Surely this will end up using more memory resources, say you have a 50% compressed piece of data in memory, to store the uncompressed data would require another 100% on top of that, effectively using 150% more then if you just had the uncompressed data in memory to begin with.  Or am I missing something?  Regardless, if you’ve ever zipped up a SQL Backup (I have, more times then I can count) then this seems like an obvious piece of “low hanging fruit” for the SQL 2008 team.  I’ve always wondered why they didn’t at least include built in compression for database backup and restore…