开发者

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 revisi开发者_Python百科on 29034) [x86_64-darwin10.4.0]

and then see if 1.9.2 is printed out. If so, i return true.

How would this method look like using a regexp?


I'd recommend using 'RUBY_VERSION', however you could do something like:

`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 ? 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)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜