我在尝试开发聊天机器人命令时遇到了一点问题
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/