我如何能简化太多和/或ruby呢

时间:2020-12-21 11:44:48

Below is my code. How to simplify this code?
I just want to avoid too many && conditions.

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

Suppose I've two 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/')
        { ... }

3 个解决方案

#1


5  

First option:

第一选择:

if @profile && (%w(a b c d e) & point.split('')).none?

Other option would be to use a regex:

其他选择是使用regex:

if @profile && !point.match(/[abcde]/)

As @Stefan pointed in comments, somewhat shorter version:

@Stefan在评论中指出:

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

As to the OP comment on the checking

关于OP的审核意见

whether the url contains '/heels/'

url是否包含'/heels/'

Since it is a specific string of characters you're looking for, I think checking for inclusion would do:

由于它是您正在查找的特定字符串,我认为检查是否包含以下内容就可以了:

if @profile && !point.include?('/heels/')

EDIT

Having a list_of_strings that you want to check for within a point, you could go with:

有一个list_of_strings需要在点内检查,您可以这样做:

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

#2


2  

You could use match with a regular expression:

您可以使用match与正则表达式:

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

#3


0  

if @profile && (/[abcde]/.match(point)).nil?

or

if @profile && (/[abcde]/ =~ point).nil?

#1


5  

First option:

第一选择:

if @profile && (%w(a b c d e) & point.split('')).none?

Other option would be to use a regex:

其他选择是使用regex:

if @profile && !point.match(/[abcde]/)

As @Stefan pointed in comments, somewhat shorter version:

@Stefan在评论中指出:

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

As to the OP comment on the checking

关于OP的审核意见

whether the url contains '/heels/'

url是否包含'/heels/'

Since it is a specific string of characters you're looking for, I think checking for inclusion would do:

由于它是您正在查找的特定字符串,我认为检查是否包含以下内容就可以了:

if @profile && !point.include?('/heels/')

EDIT

Having a list_of_strings that you want to check for within a point, you could go with:

有一个list_of_strings需要在点内检查,您可以这样做:

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

#2


2  

You could use match with a regular expression:

您可以使用match与正则表达式:

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

#3


0  

if @profile && (/[abcde]/.match(point)).nil?

or

if @profile && (/[abcde]/ =~ point).nil?