Ruby 1.8 vs LINQ
Jon Udell: I’ve been checking out the LINQ technical preview, and it’s definitely an eye-opener. The following snippet does a three-way join across an XML data source and two CLR objects. The XML data source is the content of this blog. The objects are a dictionary of date mappings, and an array of strings. The output is constructed as XML.
As an educational exercise, I’ve converted this to Ruby
doc = REXML::Document.new open("blog.xml")
d = {"2005/09"=>"September 2005", "2005/08"=>"August 2005"}
a = ["greasemonkey", "ajax"]
xml = Builder::XmlMarkup.new
REXML::XPath.match(doc,"//item").select {|item|
d.keys.find {|key| item.elements["date"].text.include? key} and
a.find {|tag| item.elements["tags"].text.include? tag}
}.sort_by {|item| item.elements["date"].text}.reverse.each {|item|
xml.item {
xml.month d[d.keys.find {|key| item.elements["date"].text.include? key}]
xml.date item.elements["date"].text
xml.title item.elements["title"].text
xml.tags item.elements["tags"].text
}
}
puts xml.to_s
Things to note:
- From a functionality point of view, the two implementations are largely equivalent (though some differences are mentioned below). This means that the differences are primarily syntax. In fact, the impression I got from this video is that C#'s LINQ support is largely syntactic sugar over roughly equivalent runtime functionality. That being said, syntax matters. Very much so. And having an SQL like syntax is a big enabler for many folks.
- Note I said “SQL like”. What in reality you
have is SQL#. In Jon’s example, I see
&&andsortby. In other examples, I have seen==. Not to mention the Yoda-like “from, where, select” . More importantly, in both Jon and my examples you are very much aware of the data sources, with things likeDescendantsandElementsprinkled throughout the code. - Despite progress being made with
vardeclarations, the C# implementation still requires the developer to be more involved with the data typing administrivia than the Ruby one does. In particular, note the(string)in the middle of the query. - Conversely, the Ruby implementation, the developer is more
involved in the administrative details associated with naming In
particular, note the three occurrences of
|item|and in building the XML, I had to say that one needs to get “tags” and call the resulting XML element “tags”. The latter could be improved with better integration between Ruby’s XML builder and REXML. - The biggest functional difference is that I had to write the code to find the month twice. LINQ’s implementation could do the same, or it could add an additional map step. While not a big deal in this particular example, it could very well be a significant issue in larger examples.
All in all, a little competition in language design is a good thing. Ruby would do well to study LINQ. And, of course, Perl 6 (if it ever ships) would allow third parties to design such syntaxes for themselves.
typo: “Conversely, [in] the Ruby implementation”
The “yoda-like” syntax is supposedly there to allow autocomplete in Visual Studio. I wonder if it makes a little dropdown based on a schema.
Posted by Robert Sayre at(X)Linq vs Ruby
The buzzwords of the moment: Linq for the .Net world, Ruby for the open source world. Jon Udel wrote the following example for the .Net world to show of the nice things about Linq: The following snippet does a three-way join across an XML data...Excerpt from Mischa Kroon at
Nicely done sir.
I had to quickly hack a port to Groovy to join in the fun :)
Cheers,
Dion
Posted by Dion Almaer atlinks for 2005-10-02
Mobile Phone As Home Computer (tags: ideas future phones usability) PyMP3Cut (tags: python mp3) SharkJumping: Podcasting Ad Insertion - Stop the Madness (tags: advertising podcasting vc startups) Sam Ruby: Ruby 1.8 vs LINQ (tags: ruby xml .net sql...Excerpt from Coty's Weblog at
SPARQLScript?
Microsoft’s LINQ is pretty cool, it allows you to address diverse data sources with the same (programmatic) query syntax. But looking over Sam Ruby’s rendition of an example in Ruby (also cool) and his comments, both versions are very...Excerpt from Raw at
eBay SOAP Update: Syntax Matters
I saw a request for some actual SOAP code, so I will try and oblige. I don’t want to publish the entire code because there’s lots of messy stuff that’s specific to eBay’s SOAP API. I’ll talk about that some other time, but for now, those details...Excerpt from Adam Trachtenberg at
native queries
for example. A solid relational foundation inside a programming language (whichever one is used) still seems to be a long way off. I sure wish I had more answers ready. ]]>...Excerpt from Jean-Claude Wippler at
Programmer Intent or What you’re not getting about Ruby and why it’s the tits
... [more]Trackback from Scott Hanselman's Computer Zen at
Programmer Intent or What you're not getting about Ruby and why it's the tits
A user named yesthatmcgurk left a comment on DotNetKicks where he/she said: I must be a complete loser, because I can’t see where Ruby is such hot shit. I’d love to read a story, “What you’re not getting about Ruby and why its the tits.” Such a...Excerpt from Scott Hanselman's Computer Zen at
Programmer Intent or What you’re not getting about Ruby and why it’s the tits
... [more]Trackback from Scott Hanselman's Computer Zen at
Programmer Intent or What you’re not getting about Ruby and why it’s the tits
... [more]Trackback from Scott Hanselman's Computer Zen at
The yoda like syntax you mention is actually how SQL server or any database parses a normal textual query. While it might be “backwards” in way of traditional select statements, to me it’s actually more intuitive. It’s like an upside down pyramid. For example, I want to get customers (from) that have the first name with “t” (where) let me get their first name’s and address (select). I feel that traditional SQL is more “yoda-like.”
Posted by Tyler Garlick at
Man your code is beautiful. Looking back at the LINQ code it’s quite obvious. In order to comprehend it, the “SQL” has to be separated into separate lines with indentation. Whereas the ruby code just flows like water...
Posted by Sharon Rosner at