Ted Neward and Glenn Vanderburg are having lines-of-code measuring contest between Ruby and Monad (MSH). They've taken Lee Holmes' clever Encarta Answers script and Glenn's written it in Ruby.
Ted says:
"...notice how we're making use of the vast power underneath the .NET framework to lay out a pretty straightforward use of the code, in a way that's entirely dynamic and loosely-typed, including the assumed return value from the if/else block, and so on."
Using Ted's own words against him, Glenn says:
[when considering lines of code/syntactic tokens]...Ruby is clearly the winner.Ruby: 17 lines, 76 tokensMonad: 37 lines, 217 tokens
Personally, I'm Switzerland in this argument. Learn them both, you'll be a better person. I know .NET better so the Monad version makes more sense to me.
One thing I would suggest that Glenn do, if he really wants to get into some fun, is take a good hard look at John Lam's RubyCLR. Sure, there've been some cursory attempts at bridging the CLR and Ruby before as well as current work to get Ruby code compiling to IL. However, for the Windows world at least, Ruby's success on our platform will be (I predict) measured by two things:
RubyCLR is elegant in that it doesn't attempt something so lofty (or perhaps ultimately tilting at windmills) as compiling Ruby to IL. Instead it does what Watir did - it opens another whole world to the already great Ruby interpreter. Will there be a Ruby/Mono bridge? There should be, and then Glenn would have something to smack Ted over the head with as then the IRB (Ruby Interactive Shell) starts looking like cross-platform Monad.
Me? I don't care, I'll just run them both in a Tabbed Console like the Console Project at SF.NET. (Note the screen shot above, it's the Console Demo 15 drop. Support this project!)
UPDATE: Lee Holmes has retorted brilliantly with a rewrite of the original get-answer script. Here's his rewrite along with the ruby version.
param([string] $question = $(throw "Please ask a question."))
[void] [Reflection.Assembly]::LoadWithPartialName("System.Web")$webClient = new-object System.Net.WebClient
$text = $webClient.DownloadString("http://search.msn.com/encarta/results.aspxq=$([Web.HttpUtility]::UrlEncode($question))")
if($text -match "<div id=`"results`">(.+?)</div></div><h2>Results</h2>") { $partialText = $matches[1] $partialText = $partialText -replace "<\s*a.*?>.+?</a>", "" $partialText = $partialText -replace "</(div|span)>", "`n" $partialText = $partialText -replace "<[^>]*>", "" $partialText = ($partialText -replace "`n`n","`n").TrimEnd() $partialText} else { "No answer found."}
And the Ruby version...
#!/usr/bin/env ruby
require 'open-uri'require 'cgi'
fail "Please ask a question." unless ARGV.size > 0question = ARGV.join(" ")query = "http://search.msn.com/encarta/results.aspx?q=#{CGI::escape(question)}"page = URI(query).read or fail "Couldn't get response."
puts
if page =~ %r{<div id="results">(.+?)</div></div><h2>Results</h2>} response = $1 response.gsub! %r{<\s*a.*?>.+?</a>}, "" # remove links response.gsub! %r{</(div|span)>}, "\n" # insert newlines response.gsub! %r{<.*?>}, "" # remove tags puts response.squeeze("\n").rstrip # collapse adjacent newlines and trim whitespaceelse puts "No answer found."end
Clearly Lines of Code is a ridiculous metric to measure just about anything, and this is more proof that both Ruby and Monad kick the llama's ass.
Ads by The Lounge