]]>ruby学习(fn)W记《二?/title>http://www.shnenglu.com/skyscribe/archive/2012/02/19/ruby-study-1.htmlskyscribeskyscribeSun, 19 Feb 2012 11:29:00 GMThttp://www.shnenglu.com/skyscribe/archive/2012/02/19/ruby-study-1.htmlhttp://www.shnenglu.com/skyscribe/comments/165993.htmlhttp://www.shnenglu.com/skyscribe/archive/2012/02/19/ruby-study-1.html#Feedback0http://www.shnenglu.com/skyscribe/comments/commentRss/165993.htmlhttp://www.shnenglu.com/skyscribe/services/trackbacks/165993.html来源Q?br />
Yukihiro Matsumoto: You can reconvert a closure back into a block, so a closure can be used anywhere a block can be used. Often, closures are used to store the status of a block into an instance variable, because once you convert a block into a closure, it is an object that can by referenced by a variable. And of course closures can be used like they are used in other languages, such as passing around the object to customize behavior of methods. If you want to pass some code to customize a method, you can of course just pass a block. But if you want to pass the same code to more than two methods -- this is a very rare case, but if you really want to do that -- you can convert the block into a closure, and pass that same closure object to multiple methods.
7U结?/h2>
block方式3U?/li>
隐式传入Q内部用yield调用
def thrice yield yield yield
end
x=1
thrice {x+=2}
&block参数传入Q内部直接操?amp;block
def six_times(&block)
thrice(&block)
thrice(&block)
end
x =4
six_times { x +=10 }
&block传入Q保存block为变量,然后调用block.call
def save_for_later(&b)
@saved = b # Note: no ampersand! This turns a block into a closure of sorts. end
save_for_later { puts "Hello!" }
puts "Deferred execution of a block:"
@saved.call
@saved.call
q里的saved保存?sh)main对象的一个成员,后边实现延迟调用?br />
Proc.new/proc
@saved_proc_new = Proc.new { puts "I'm declared with Proc.new." }
@saved_proc = proc { puts "I'm declared with proc." }
@saved_proc_new.call
@saved_proc.call
lambda
@saved_lambda =lambda { puts "I'm declared with lambda." }
@saved_lambda.call
method
def some_method
puts "I'm declared as a method."
end
@method_as_closure = method(:some_method)
]]>Unix文本处理之利器-awk/gawkhttp://www.shnenglu.com/skyscribe/archive/2011/01/02/awk.htmlskyscribeskyscribeSun, 02 Jan 2011 12:32:00 GMThttp://www.shnenglu.com/skyscribe/archive/2011/01/02/awk.htmlhttp://www.shnenglu.com/skyscribe/comments/137894.htmlhttp://www.shnenglu.com/skyscribe/archive/2011/01/02/awk.html#Feedback0http://www.shnenglu.com/skyscribe/comments/commentRss/137894.htmlhttp://www.shnenglu.com/skyscribe/services/trackbacks/137894.htmlsince 1977Q。作者中最引h注目的当属鼎鼎大名的Brian W. Kernighan (即K的简U来源)(j)。目前所用的版本大多是gawk或者nawk.
]]>Popen中奇怪的OSErrorQtoo many open fileshttp://www.shnenglu.com/skyscribe/archive/2009/10/19/98970.htmlskyscribeskyscribeMon, 19 Oct 2009 11:31:00 GMThttp://www.shnenglu.com/skyscribe/archive/2009/10/19/98970.htmlhttp://www.shnenglu.com/skyscribe/comments/98970.htmlhttp://www.shnenglu.com/skyscribe/archive/2009/10/19/98970.html#Feedback0http://www.shnenglu.com/skyscribe/comments/commentRss/98970.htmlhttp://www.shnenglu.com/skyscribe/services/trackbacks/98970.htmlq来用Python的unittest框架来做一个regression test 工具Q验证一些复杂的E序功能Q期望做到自动化试完成一部分需要繁琐的界面点击和等待才能完成的验证工作?/font>