[英]Check if a command prints out a string that matches a regexp with Ruby?
I want to run: 我要跑步:
> ruby --version
ruby 1.9.2p0 (2010-08-18 revision 29034) [x86_64-darwin10.4.0]
and then see if 1.9.2
is printed out. 然后查看是否打印出1.9.2
。 If so, i return true
. 如果是这样,我返回true
。
How would this method look like using a regexp? 使用正则表达式该方法看起来如何?
我建议使用“ RUBY_VERSION”,但是您可以执行以下操作:
`ruby --version`.include? "1.9.2"
RUBY_VERSION == "1.9.2"
Maybe I am off the track here but you do seem to want to check the version from the shell ? 也许我不在这里,但您似乎确实想从shell检查版本? Something like this then will do it. 这样的事情就可以了。
export VERSION=`ruby --version | grep 1.9.2`
if [[ -n "$VERSION" ]] ; then
echo "you have the right version yay!"
else
echo "bummer dude ><!"
fi
The regex for this is simply 正则表达式很简单
/1.9.2/
So 所以
s=`ruby --version`
return true if s=~/1\.9\.2/
(updated) (更新)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.