如何在ruby中简化太多和/或

如何在ruby中简化太多和/或,ruby,Ruby,下面是我的代码。如何简化此代码? 我只是想避免太多的&&条件 假设我有两个url: ipt_string = www.abc.com/catogories/profile/heels/ ipt_string = www.abc.com/me/payment/auth/ def to_uri(ipt_string) point = ipt_string.gsub('{', '#{') if @profile &&

下面是我的代码。如何简化此代码?
我只是想避免太多的
&&
条件

假设我有两个url:

ipt_string = www.abc.com/catogories/profile/heels/

ipt_string = www.abc.com/me/payment/auth/

    def to_uri(ipt_string)
        point = ipt_string.gsub('{', '#{')
        if @profile && 
           !point.include?('/catogories/') && 
           !point.include?('/profile/') && 
           !point.include?('/heels/') && 
           !point.include?('/me/') && 
           !point.include?('/payment/') && 
           !point.include?('/auth/')
        { ... }
第一种选择:

if @profile && (%w(a b c d e) & point.split('')).none?
另一种选择是使用正则表达式:

if @profile && !point.match(/[abcde]/)
正如@Stefan在评论中指出的,略短的版本:

if @profile && point !~ /[abcde]/

至于OP对检查的评论

url是否包含
'/heels/'

因为它是您要查找的特定字符串,所以我认为检查是否包含:

if @profile && !point.include?('/heels/')
编辑 在
内有要检查的
字符串列表
,您可以选择:

if @profile && list_of_strings.none? { |str| point.include?(str) }

可以对正则表达式使用
match

> "a string of text".match(/[abcde]/).nil?
 => false
> "a string of text".match(/[zq]/).nil?
 => true


def to_uri(ipt_string)point=ipt_string.gsub('{','#{'))它是一个字符串吗?是的。如果我点击uri..uri是否包含所有字符串。只想检查这个点击uri?所有字符串?请提供一些上下文,我不知道你在说什么:-)假设我点击了uri的端点..我想检查所提到的字符串是否存在于该端点上,而不是字符串w如果没有空格,第一次测试就会失败,不是吗?@lcguida确实应该是
。拆分(“”)
,编辑,谢谢!@Stefan确实更短:)关于您的编辑:我假设每个字符串实际上都是一个路径段。@J.Adhikari是的,只需创建一个数组:字符串列表=['foo','bar','baz'],我建议
'pet!/[abcde]/#=>false;'pot'!~/[abcde]/#=>true
if @profile && (/[abcde]/.match(point)).nil?
if @profile && (/[abcde]/ =~ point).nil?