monad_Ruby作为跨平台Monad

博客讨论了TedNeward和GlennVanderburg之间的代码量比较,他们在Ruby和Monad(MSH)之间进行了一场比赛。LeeHolmes的Encarta Answers脚本被用作示例,展示了Ruby代码行更少、更简洁。尽管作者认为代码行数不是衡量语言优劣的适当标准,但他提到了RubyCLR作为Ruby与.NET交互的一个强大工具,并提出了构建Ruby/Mono桥的可能性。
摘要由CSDN通过智能技术生成
monad

monad

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 NewardGlenn Vanderburg正在Ruby和Monad(MSH)之间进行代码行测量竞赛。 他们采用了Lee Holmes的巧妙的Encarta Answers脚本,Glenn用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."

“ ...请注意,我们如何利用.NET框架下方的强大功能以完全动态且宽松类型的方式(包括if的假定返回值)对代码进行非常简单的使用/ else块,依此类推。”

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 tokens
Monad: 37 lines, 217 tokens

[考虑代码行/语法标记时] ... Ruby显然是赢家。 Ruby:17行,76个令牌Monad:37行,217个令牌

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.

就个人而言,我是瑞士。 两者都学习,你将成为一个更好的人。 我更了解.NET,所以Monad版本对我来说更有意义。

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:

如果他真的想找点乐子,我建议格伦做的一件事就是仔细看一下约翰·林( John Lam)的RubyCLR。 当然,在将CLR和Ruby桥接之前,已经进行了一些粗略的尝试,并且目前正在进行将Ruby代码编译为IL的工作。 但是,至少对于Windows世界来说,Ruby在我们平台上的成功将(由我预测)由两方面来衡量:

  • Watir - A Ruby Interop layer on top of IE (and now Firefox with FireWatir)

    Watir -IE(现在是Firefox和FireWatir )之上的Ruby Interop层

  • John's RubyCLR - I'm convinced that John is doing the whole world a service with this bridge.

    John的RubyCLR-我坚信John正在使用此桥为全世界提供服务。

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.

RubyCLR非常优雅,因为它不会尝试像将Ruby编译为IL那样崇高(或最终可能会在风车上倾斜)的东西。 相反,它做了Watir所做的事情-它为本已很好的Ruby解释器打开了另一个世界。 会有Ruby / Mono桥吗? 应该有,然后Glenn会想在Ted上敲一些东西,然后IRB (Ruby Interactive Shell)开始看起来像跨平台的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!)

我? 我不在乎,我只会在选项卡式控制台(如SF.NET的控制台项目)中同时运行它们。 (请注意上面的屏幕截图,这是Console Demo 15下拉菜单。支持此项目!)

UPDATE: Lee Holmes has retorted brilliantly with a rewrite of the original get-answer script. Here's his rewrite along with the ruby version.

更新: Lee Holmes对原始的get-answer脚本进行了出色的反驳。 这是他的重写以及Ruby版本。

param([string] $question = $(throw "Please ask a question."))

param([字符串] $ question = $(抛出“请问一个问题。”))

[void] [Reflection.Assembly]::LoadWithPartialName("System.Web")
$webClient = new-object System.Net.WebClient

[void] [Reflection.Assembly] :: LoadWithPartialName(“ System.Web”) $ webClient =新对象System.Net.WebClient

$text =  $webClient.DownloadString("http://search.msn.com/encarta/results.aspxq=$([Web.HttpUtility]::UrlEncode($question))")

$ text = $ webClient.DownloadString(“ http://search.msn.com/encarta/results.aspx q = $([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."
}

if($ text -match“ <div id =`” results`“>(。+?)</ div> </ div> <h2>结果</ h2>”) { $ partialText = $ matches [1] $ partialText = $ partialText-替换“ <\ s * a。*?>。+?</a>”,“” $ partialText = $ partialText -replace“ </(div | span)>”,“`n” $ partialText = $ partialText-替换“ <[^>] *>”,“” $ partialText =($ partialText -replace“`n`n”,“`n”)。TrimEnd() $ partialText } 其他{ “找不到答案。” }

And the Ruby version...

还有Ruby版本...

#!/usr/bin/env ruby

#!/ usr / bin / envRuby

require 'open-uri'
require 'cgi'

需要'open-uri' 需要'cgi'

fail "Please ask a question." unless ARGV.size > 0
question = ARGV.join(" ")
query = "
http://search.msn.com/encarta/results.aspx
?q=#{CGI::escape(question
)}"
page = URI(query).read or fail "Couldn't get response."

失败“请提出问题”。 除非ARGV.size> 0 问题= ARGV.join(“”) 查询=“ http://search.msn.com/encarta/results.aspx ?q =#{CGI :: escape(question }}” 页面= URI(query).read或失败“无法获得响应。”

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 whitespace
else
  puts "No answer found."
end

如果页面=〜%r {<div id =“ results”>(。+?)</ div> </ div> <h2>结果</ h2>} 响应= $ 1 response.gsub! %r {<\ s * a。*?>。+?</a>},“”#删除链接response.gsub! %r {</(div | span)>},“ \ n”#插入换行符response.gsub! %r {<。*?>},“”#删除标签将response.squeeze(“ \ n”)。rstrip#折叠到相邻的换行符并修剪空白其他放置“未找到答案”。 结束

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.

显然,“代码行”是衡量几乎所有内容的荒谬指标,这更多地证明了Ruby和Monad都在踢骆驼。

翻译自: https://www.hanselman.com/blog/ruby-as-cross-platform-monad

monad

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值