Thursday, October 28, 2010

Code Generation in Ruby

While doing the code generation exercise (chapter 3, code generators) in the book The Pragmatic Programmer, I realized it quickly that C# isn't the best language to do code generation. The main block was that it is too verbose to do regular expression on strings. I had to use Regex.IsMatch and then Regex.Split to get the the tokens. Thats when I looked into ruby.

Regular expressions is a built in feature of Ruby, put between two forward slashes (/). You can use =~ operator for a regex match which sets the special variables $~.  $& holds the text matched by the whole regular expression. $1, $2, etc. hold the text matched by the first, second, and following capturing groups. Some good info here.

class CSharpCode
 def BlankLine
  print "\n"
 end
 
 def Comment comment 
  print "// #{comment}\n"
 end 
 
 def StartMsg name
  print "public struct #{name} {\n"
 end
 
 def EndMsg
  print "}\n"
 end
 
 def SimpleType name, type
  print "\t#{type} #{name};\n"
 end
 
 def ComplexType name, type, size
  if(type == "char[")
   type = "string"
   print "\t#{type} #{name};\n"
  end
 end

end

if __FILE__ == $0
 unless ARGV[0]
  print "cml usage: cml Input.txt\n"
  exit
 end

 if(File.exist?(ARGV[0]))
  CG = CSharpCode.new
  File.open(ARGV[0]).each_line { |line|
   line.chomp!;
   
   if(line =~ /^\s*S/)
    CG.BlankLine
   elsif line =~ /^\#(.*)/
    CG.Comment $1
   elsif line =~ /^M\s*(.+)/
    CG.StartMsg $1
   elsif line =~ /^E/
    CG.EndMsg
   elsif line =~ /F\s*(\w+)\s*(\w+\[)(\d+)\]/
    CG.ComplexType $1, $2, $3
   elsif line =~ /^F\s+(\w+)\s*(\w+)/
    CG.SimpleType $1, $2
   else
    print "Invalid line"
   end
  }
 end
end

Input File
 # Add a product
 # to the 'on-order' list
 M AddProduct
 F id   int
 F name   char[30]
 F order_code int
 E
Output
//  Add a product
//  to the 'on-order' list
public struct AddProduct {
 int id;
 string name;
 int order_code;
}

Wednesday, October 27, 2010

Playing with Google Charts

I wanted to insert a quick chart in my blog using charts from Google Docs after reading about new editor features. It turned out to be very easy. The best thing is that the editor automatically suggests a chart for you when you select data. And with charts, it is easy to visualize that my blog postings are on rise!

Tuesday, October 26, 2010

Hello to RubyMine

As part of starting my adventures in Ruby, I thought an IDE is essential and so settled with RubyMine (on a 30 day trial.) As my first program in ruby I just wanted to print "Hello World" from a static method.


The trick to use was keyword self. The scope of self varies depending on the context. Inside an instance method, self resolves to the receiver object. Outside an instance method, but inside a class definition, self resolves to the class.

class Hello
  def self.SayHello()
    puts "Hello World"
  end
end

Hello.SayHello;

And self is not the only way to create class methods. Here is a list of more.

Thursday, October 21, 2010

Code Contracts

I recently downloaded Code Contracts from Microsoft. I got introduced to Code Contracts in Pragmatic Programmer and then got a better understanding of it from Jon Skeet's C# In Depth while the chapter was freely available (unfortunately, it is not now.) Code Contracts is another tool which should have been here a long while ago in .net world.

In Code Contracts, Preconditions are specified using Contract.Requires to specify constraints on the input to a method. Postconditions are used to specify express constraints on the output of a method using Contract.Ensures. Here is a code snippet from C# in Depth, Chapter 15, which shows code contract in action

static int CountWhitespace(string text) 

    Contract.Requires(text != null, "text"); 
    Contract.Ensures(Contract.Result() >= 0); 
    return text.Count(char.IsWhiteSpace); 
}

Sunday, October 10, 2010

From Nu to NuPack

At last Microsoft gave some good news in the series of bad technologies they rolled out recently - WebMatrix, Lightswitch, NuPack. NuPack is good. It will be used - both by newbies and the veterans. Why? Because it makes life easier for developers and doesn't dumb you down. Inspired from Ruby gems, Nu had gotten some traction as package management system. I have used it and loved it but its new avatar as NuPack is even awesome. Integrated into Visual studio as power shell, it is addictive and I bet, as a positive side effect, we will start using shell more. When you add a package, it adds a reference and merges everything it needs in web.config. For a detail Introduction please follow the scott hanselman's Introducing NuPack Package Management for .NET - Another piece of the Web Stack

NuPack arrived with an interesting debugging tale by Tatham Oddie. It was a great post the way he narrowed down the exception using WinDbg and Reflector.

When I downloaded NuPack, the first I wanted to download was castle ActiveRecord, which unfortunately is not in the package list but there are ELMAH, Castle.Core, Catle.Ioc etc. So start using it and start saving your time.

Scott Gu's Announcing NuPack, ASP.NET MVC 3 Beta, and WebMatrix Beta 2 gives some good links for NuPack and Rob Reynold’s “Evolution of Package Management on .NET” Post (Rob is one of the leaders of the Nu project and is on the NuPack team) gives a good account of whats happening with Nu and NuPack.