自己常用的ruby小函数,贴一下

作者:半瓶墨水   链接:http://www.2maomao.com/blog/ruby-small-general-procs/
下载: ggg.rb
#does ruby guy notice about this pain?
def reverse_slash(str)
    
str.gsub(/\//) { |x| "\\" }
end
 
#double-quote str
def dquote(str)
    
"\"#{str}\""
end
 
#execute and wait for each line
def execute_and_watch(str)
    
putsflush "  command:#{str}"
    
IO.popen(str).each {|line| yield line}
end
 
#log to the $stderr
def logerr(str)
    
#we might need to set the error log file and output log file in the future
    
$stderr.putsflush "ERROR! " + str
end
 
#puts to the $stdout and flush
def putsflush(*str)
    
puts *str
    
$stdout.flush
end

留下回复