ruby - `include?' 中没有将 false 隐式转换为 String>

标签 ruby typeerror

我在尝试开发聊天机器人命令时遇到了一点问题

match /^kill (.*)|^kill/, :method => :kill 
   def kill(user, target)
      target = target
      name = user.name
       if target.to_s.include? name || target.to_s.empty?
         require 'erb' 
         phrases = ["<%= name %> (gun) Se pegó un tiro porque el nestea con cloro no funcionó"]
         name = user.name
         target = target
         random_phrase = ERB.new(phrases.sample).result(binding)
         @client.send_msg random_phrase 
       elsif target.include? ['chat', 'todo', 'mundo']
         require 'erb' 
         phrases = ["<%= name %> se arrechó y le cayó a tiros a todo el mundo (gun)"]
         name = user.name
         target = target
         random_phrase = ERB.new(phrases.sample).result(binding)
         @client.send_msg random_phrase 
       else 
        require 'erb' 
         phrases = ["<%= name %> (gun) le pegó un tiro a <%= target %> por haberle robado memes"]
         name = user.name
         target = target
         random_phrase = ERB.new(phrases.sample).result(binding)
         @client.send_msg random_phrase 
        end
     end

这里我有两个错误,但我不知道如何解决它们

#<TypeError: no implicit conversion of false into String> in `include?'
#<TypeError: no implicit conversion of Array into String> in `include?

我已经尝试了很多东西,但我没能成功。

最佳答案

请注意,String#include? 采用 String,因此如果您的参数不是 String,它会将您的参数转换为 String。

第一个错误 no implicit conversion of false into String 来自 target.to_s.include? name && target.to_s.empty?.根据ruby's operators precedence ,将首先计算导致 bool 值传递给 include? 函数的 AND 运算符。您可以通过修改为 target.to_s.include?(name) && target.to_s.empty? 来解决此问题。

第二个,你应该使用Array#include? 而不是String#include?。你可以这样写:['chat', 'todo', 'mundo'].include?目标

关于ruby - `include?' 中没有将 false 隐式转换为 String>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41497830/

相关文章:

javascript - 数据表不显示 Django

ruby - Treetop ruby​​ 解析器 - 无法解析 Ordered Choice

ruby-on-rails - ruby 'mikel/mail' 回复字段

python - Python-使用 “FLANN”- “Type Error”

javascript - 绘制图像不绘制

Python:TypeError:* 之后的参数必须是一个序列

javascript - 为什么我的 if 条件不能阻止我的脚本崩溃?

ruby - 为什么这个 while 循环内外的变量这个词不同?

ruby-on-rails - 如果还有错误

ruby-on-rails - 将 acts_as_list 与 has_many 一起使用 :through in rails