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

推荐订阅源

TaoSecurity Blog
TaoSecurity Blog
Jina AI
Jina AI
雷峰网
雷峰网
月光博客
月光博客
The GitHub Blog
The GitHub Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
美团技术团队
C
CXSECURITY Database RSS Feed - CXSecurity.com
小众软件
小众软件
Security Latest
Security Latest
Microsoft Azure Blog
Microsoft Azure Blog
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
C
Cybersecurity and Infrastructure Security Agency CISA
Last Week in AI
Last Week in AI
A
Arctic Wolf
Latest news
Latest news
Attack and Defense Labs
Attack and Defense Labs
I
Intezer
F
Fortinet All Blogs
罗磊的独立博客
MongoDB | Blog
MongoDB | Blog
Webroot Blog
Webroot Blog
S
Secure Thoughts
Help Net Security
Help Net Security
Apple Machine Learning Research
Apple Machine Learning Research
博客园_首页
V
Visual Studio Blog
P
Proofpoint News Feed
博客园 - 【当耐特】
P
Privacy International News Feed
V
Vulnerabilities – Threatpost
Stack Overflow Blog
Stack Overflow Blog
Know Your Adversary
Know Your Adversary
云风的 BLOG
云风的 BLOG
Hacker News: Ask HN
Hacker News: Ask HN
L
LINUX DO - 最新话题
H
Help Net Security
爱范儿
爱范儿
酷 壳 – CoolShell
酷 壳 – CoolShell
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
量子位
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
T
Tenable Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Hugging Face - Blog
Hugging Face - Blog

博客园 - 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]}

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