2017-01-06 28 views
0

我買了,我試圖開發在include中沒有隱式將false轉換爲String>。

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? 

我已經嘗試了很多東西,但我不管理這個工作。

回答

3

請注意String#include?需要字符串,以便它將您的參數轉換爲字符串,如果它不是字符串。第一個錯誤no implicit conversion of false into String來自target.to_s.include? name && target.to_s.empty?。根據ruby's operators precedence,首先計算AND運算符,導致布爾值傳遞給您的include?函數。您可以通過修改爲target.to_s.include?(name) && target.to_s.empty?來解決此問題。

第二個,你應該使用Array#include?而不是String#include?。你可以這樣寫:['chat', 'todo', 'mundo'].include? target

0

name && target.to_s.empty?返回布爾值,然後傳遞給include?。但是,String#include?String作爲參數,而不是布爾值。

+0

錯誤發生在 'if target.to_s.include?名稱&& target.to_s.empty?' 至少第一個 '#<類型錯誤:在include中沒有隱式轉換爲字符串> –

相關問題