开发者

Check if a command prints out a string that matches a regexp with Ruby?

开发者 https://www.devze.com 2023-01-13 21:30 出处:网络
I want to run: > ruby --version ruby 1.9.2p0 (2010-08-18 revisi开发者_Python百科on 29034) [x86_64-darwin10.4.0]

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

精彩评论

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

关注公众号