惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V2EX - 技术
V2EX - 技术
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Latest news
Latest news
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
L
Lohrmann on Cybersecurity
aimingoo的专栏
aimingoo的专栏
B
Blog
T
Threat Research - Cisco Blogs
罗磊的独立博客
Application and Cybersecurity Blog
Application and Cybersecurity Blog
P
Proofpoint News Feed
P
Palo Alto Networks Blog
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
宝玉的分享
宝玉的分享
博客园 - 司徒正美
Google DeepMind News
Google DeepMind News
Blog — PlanetScale
Blog — PlanetScale
T
Tor Project blog
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
Martin Fowler
Martin Fowler
酷 壳 – CoolShell
酷 壳 – CoolShell
Recorded Future
Recorded Future
D
DataBreaches.Net
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
IT之家
IT之家
B
Blog RSS Feed
Scott Helme
Scott Helme
P
Proofpoint News Feed
V
Vulnerabilities – Threatpost
A
Arctic Wolf
Help Net Security
Help Net Security
L
LINUX DO - 最新话题
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
Vercel News
Vercel News
AWS News Blog
AWS News Blog
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Schneier on Security
Hacker News: Ask HN
Hacker News: Ask HN
N
Netflix TechBlog - Medium
L
LangChain Blog
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
M
MIT News - Artificial intelligence
N
News and Events Feed by Topic
Webroot Blog
Webroot Blog
W
WeLiveSecurity

博客园 - Tachikoma

用Ruby实现 Web Service Server ,并用Ruby发送 HTTP请求 Web Service Android 使用 Ksoap2 出现的低级错误... - Tachikoma Cygwin中通过RJB在Ruby下调用ICTCLAS(JAVA) ICTCLAS4J 的编译脚本 Ruby Sandbox 实现运行客户代码 - Tachikoma Ruby在使用MongoDB时,对Cursor的重新包装 ruby 下使用 ICTCLAS(JAVA) RJB 在windows下的一些安装事项 Ruby手工测试正确,rcov测试失败的解决 在dell dimension 5150 上安装 leopard 手记 Rails测试中清空数据表/载入空fixtures RDoc 解决同名module 与 Class的问题 C 实用的 e-editor 的bundle Best of Ruby Quiz - Animal Quiz Best of Ruby Quiz - GEDCOM Parser 使用selenium-on-rails的一些讨论 3]assertXpathCount的使用 使用selenium-on-rails的一些讨论 2]清理缓存 - Tachikoma - 博客园 使用selenium-on-rails的一些讨论 [0,1] 关于 rails ActiveRecord 属性 以及 foreign_key 不直接用数据库项目 时的一些讨论
Best of Ruby Quiz - MadLib - Tachikoma
Tachikoma · 2008-07-22 · via 博客园 - Tachikoma

 题目大意明确,主要在于正则表达式运用

1]

首先是自己写的程序

属于比较正常的算法型想法,流式替换过程

2]

然后是书上的程序,最简短的那个

1str = %q{Our favorite language is ((gem:a gemstone)). We think ((gem)) is 
2better than ((a gemstone)). }

3hash = Hash.new do |hash,key|
4    print "Give Me #{key.sub /\A([^:]+):/,"" } :"
5    #p $1
6    hash[$1= gets.chomp
7end
8puts str.gsub(/\(\(([^\)]+)\)\)/){hash[$1]}
9#puts str.gsub(/\(\(([^\)]+)\)\)/ , hash[$1])

 稍微有些出入,大体一样

做一些分析:

[0]这个程序存在BUG,如果把第五行注释解开,可以看到输出的第一个$1 = "gem" ,第二个$2 = "nil",BUG在后面我的改进里有修正

[1]第八行用gsub的Block形式十分重要,以下是Programming Ruby的节选

gsub 
带有block的形式,当前的匹配会作为参数传入block,并且会适当设置$1,$2,$`,$&,$'等变量

 如果用第9行的形式,会出现$1 = nil 的错误,因为用参数形式$1不能正确配置

 修正后的程序

1str = %q{Our favorite language is ((gem:a gemstone)). We think ((gem)) is 
2better than ((a gemstone)). }

3hash = Hash.new do |hash,key|
4    print "Give Me #{key.sub /^([^:]+):/,"" } :"
5    #p $1 || key
6    hash[$1 || key] = gets.chomp
7end
8puts str.gsub(/\(\(([^\)]+)\)\)/){hash[$1]}

 修正的结果可以将注释行解开验证