我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的较大程序的一部分,以及在rails环境中.
在某些情况下,我需要根据其位置对代码进行细微更改,并且我意识到以下样式似乎有效:
print "Testing nested functions defined\n"
CLI = true
if CLI
def test_print
print "Command Line Version\n"
end
else
def test_print
print "Release Version\n"
end
end
test_print()
Run Code Online (Sandbox Code Playgroud)
这导致:
Testing nested functions defined
Command Line Version
Run Code Online (Sandbox Code Playgroud)
我从未遇到过在Ruby中有条件定义的函数.这样做安全吗?
这不是我构建大部分代码的方式,但是有一些函数需要每个系统完全重写.
我正在研究一个简短的bash脚本来从curl响应中获取JSON元素.
curl -H "api_key:[API_PASSWORD]" http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false
Run Code Online (Sandbox Code Playgroud)
收益:
[{"id":0,"seq":0,"raw":"?b?t(?)n","rawType":"IPA"},{"id":0,"seq":0,"raw":"?b?t(?)n","rawType":"IPA"}]
Run Code Online (Sandbox Code Playgroud)
我正试图提取"bʌt(ə)n"元素.
虽然我不熟悉正则表达式,但我认为我应该使用这个字符串替换:
/.*"(.*)",/
Run Code Online (Sandbox Code Playgroud)
我正在尝试运行以下命令,但它似乎不起作用:
curl -H "api_key:[API_KEY]" http://api.wordnik.com/v4/word.json/button/pronunciations?sourceDictionary=macmillan&typeFormat=IPA&useCanonical=false | sed /.*"(.*)",\1/
Run Code Online (Sandbox Code Playgroud)
我确定有一些事情我做错了,经过几个小时的搜索和阅读正则表达式和bash我没有选择.
我不需要使用sed,我只是在bash命令行中寻找一种快速的方法,这样我就可以在mac上的TextExpander脚本中实现它.
我正在尝试使用运行ruby脚本nohup:
nohup ruby script.rb和
这需要几个小时才能运行,但是会通过记录其进度puts。
通常,我可以查看nohup.out以查看使用nohup运行的任何内容的最新输出。但是,我的ruby脚本在完成或被杀死之前似乎不会输出任何内容。
我究竟做错了什么?