红宝石:未定义的方法排除?

Mes*_*Qin 1 ruby

我尝试使用相反的include方法吗?在ruby中,它是“ string | array.exclude?”,这是我在交互式ruby上的代码。

this = "nice"
puts this.exclude? "n"
Run Code Online (Sandbox Code Playgroud)

但是,它总是声明一个错误。

追溯(最近一次通话):

    4: from C:/Ruby26-x64/bin/irb.cmd:31:in `<main>'
    3: from C:/Ruby26-x64/bin/irb.cmd:31:in `load'
    2: from C:/Ruby26-x64/lib/ruby/gems/2.6.0/gems/irb-1.0.0/exe/irb:11:in `<top (required)>'
    1: from (irb):14
Run Code Online (Sandbox Code Playgroud)

NoMethodError(未定义的方法“排除?”的“ nice”:字符串)

我做了很多研究,但没有找到解决方案。我不知道这是否只会发生在我身上,这让我非常烦恼。

有人可以帮我吗。非常感谢你。

Iva*_*sov 5

String#exclude?是 activesupport gem 的一部分,它安装在每个 Rails 项目中。它不包含在 ruby​​ 的标准库中。但是这个方法很简单

def exclude?(string)
  !include?(string)
end
Run Code Online (Sandbox Code Playgroud)


man*_*lli 5

尝试一下

unless this.include?
Run Code Online (Sandbox Code Playgroud)