使用逻辑选项处理搜索关键字

时间:2018-05-22 06:27:18

标签: ruby string

我想要处理一系列搜索关键字,其中包含选项"not""gis and gjs not gkp""and",例如"or",以便对每个关键字执行搜索,以及记录搜索逻辑。

为此,我需要按"not"{and: 'gis', and: 'gjs', not: 'gkp'}contest(mymodel , L, rhs = 0, joint = TRUE, collect = TRUE, confint = TRUE, level = 0.95, check_estimability = FALSE, ddf = c("Satterthwaite", "Kenward-Roger", "lme4"), ...) 分解搜索字符串,并记住每个点使用的分隔符。我想将它转换为哈希:Error in contest(mymodel, L, rhs = 0, joint = TRUE, collect = TRUE, confint = TRUE, : could not find function "contest"

最好的方法是什么?或者有更好的方法来处理这样的搜索字符串吗?

1 个答案:

答案 0 :(得分:1)

splitted = "hello and world or jump not read".split(/( and | or | not )/)
# => ["hello", " and ", "world", " or ", "jump", " not ", "read"]

splitted = splitted.unshift('and') unless splitted[0] == 'and'
# => ["and". "hello", " and ", "world", " or ", "jump", " not ", "read"]

splitted.each_slice(2).to_h
# => {"and"=>"hello", " and "=>"world", " or "=>"jump", " not "=>"read"}