如何测试字符串是否与Ruby中的glob相匹配? [英] How do I test whether a string would match a glob in Ruby?

查看:45
本文介绍了如何测试字符串是否与Ruby中的glob相匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在不访问文件系统的情况下,是否有可能查看glob "foo*"是否与Ruby中的"food"相匹配?

Without hitting the filesystem, is it possible to see whether the glob "foo*" would match "food" in Ruby?

背景:我的一个脚本生成了文件,并且我想进行单元测试,以便其他脚本能够使用其当前的glob来检测此类文件.

Background: one of my scripts produce files, and I'd like to unit test that other scripts would be able to detect such files with their current glob.

推荐答案

是的,可以使用 fnmatch 方法:

Yes, it is possible using the fnmatch method:

File.fnmatch("foo*", "food") #=> true

这篇关于如何测试字符串是否与Ruby中的glob相匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆