【问题标题】:no implicit conversion of false into String> in `include?'在 `include?' 中没有将 false 隐式转换为 String>
【发布时间】:2017-05-20 18:02:21
【问题描述】:

我正在尝试开发的聊天机器人命令有点问题

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?

我已经尝试了很多事情,但我无法做到这一点。

【问题讨论】:

    标签: ruby typeerror


    【解决方案1】:

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

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

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

    【讨论】:

      【解决方案2】:

      name &amp;&amp; target.to_s.empty? 返回一个布尔值,然后将其传递给include?。但是,String#include?String 作为参数,而不是布尔值。

      【讨论】:

      • 错误发生在if target.to_s.include? name &amp;&amp; target.to_s.empty?至少第一个#&lt;TypeError: no implicit conversion of false into String&gt; in include?'`
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-14
      • 2016-11-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多