如何使用Ruby on Rails将字符串作为方法参数?
- 内容介绍
- 文章标签
- 相关推荐
本文共计204个文字,预计阅读时间需要1分钟。
是否存在通过字符串操作创建条件?这是我将字符串作为条件传递给include代码的方法。例如:`'hello|everyone|in|the|house'.include?('hello world new york place')` 包括条件吗?
有没有办法通过字符串操作创建条件.这里是我尝试将字符串作为条件传递给include的代码?方法."hello eveyone in the house".include?(%w('hello' 'world' 'new\ york' 'place').join(" || ").to_s) 包含条件的参数?是不可能的,因为包括?只接受一个字符串参数.
但你可以这样写:
['hello', 'world', 'new york', 'place'].any? { |word| "hello everyone in the house".include?(word) }
或者您可以从字符串生成正则表达式:
"hello eveyone in the house".match?( /#{['hello', 'world', 'new york', 'place'].join('|')}/ )
本文共计204个文字,预计阅读时间需要1分钟。
是否存在通过字符串操作创建条件?这是我将字符串作为条件传递给include代码的方法。例如:`'hello|everyone|in|the|house'.include?('hello world new york place')` 包括条件吗?
有没有办法通过字符串操作创建条件.这里是我尝试将字符串作为条件传递给include的代码?方法."hello eveyone in the house".include?(%w('hello' 'world' 'new\ york' 'place').join(" || ").to_s) 包含条件的参数?是不可能的,因为包括?只接受一个字符串参数.
但你可以这样写:
['hello', 'world', 'new york', 'place'].any? { |word| "hello everyone in the house".include?(word) }
或者您可以从字符串生成正则表达式:
"hello eveyone in the house".match?( /#{['hello', 'world', 'new york', 'place'].join('|')}/ )

