一些Ruby小函数

#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
#输出到ERR,立即刷新
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

 

你可能感兴趣的:(Ruby)