C# example of blocks (loosely based upon Rob's examples):
//the action argument is the C# version of a block
// - by specifying Action<Artist> I'm stating that the block
// - will take 1 parameter, of type Artist
public void ParseKey(XDocument doc,
string elName,
Action<Artist> block)
{
//using XLinq instead of Nokogiri... *jealous*
foreach(var node in doc.Elements(elName))
{
var artist = new Artist{
Name = node.Element("Name").Value,
ArtistId = (int)node.Element("ArtistId").Value
};
block(artist);
}
}
//called by:
public void Main()
{
var doc = XDocument.Load("..\db\artists.xml");
ParseKey(doc, "Artist", artist=>{
//this is an anonymous method, the equiv
// of a Ruby block. "artist=>" is the equiv
// of |artist|
Db.Save(artist);
Console.WriteLine(artist.Name);
});
}
Certainly it is a bit cleaner in Ruby by way of:1) Nokogiri is super awesome.
2) Ruby's dynamicness means we don't have to have type ceremony everywhere.
That said, they're pretty equivalent in terms of functionality and expressiveness to me.