Rails 粉丝熟悉 params[:terms]
或从 url 收集的传递给 Controller 的“事物”散列。例如:
params
=> {"term"=>"Warren Buffet",
"controller"=>"search",
"format"=>"json",
"action"=>"index"}
如果我想在下面的代码中使用“Warren Buffet”、“Warren”和“Buffet”,有人知道我应该使用哪种方法吗? gsub
很接近,但它接受每个匹配项而不是原始字符串。除非我做错了,这是完全有可能的:
@potential_investors = [] params[:term].gsub(/(\w{1,})/) do |term| @potential_investors << User.where(:investor => true) .order('first_name ASC, last_name ASC') .search_potential_investors(term) end
想法?
最佳答案
怎么样:
s = "Filthy Rich"
s.split(" ").push(s)
>> ["Filthy", "Rich", "Filthy Rich"]
或者如果您更喜欢使用正则表达式,则使用 scan
:
s.scan(/\w+/).push(s)
>> ["Filthy", "Rich", "Filthy Rich"]
关于ruby - 使用 gsub 和 block 时传递原始字符串 PLUS 匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10791195/