简体   繁体   English

如何在Rails的字符串类中定义布尔方法?

[英]How Do I Define a Boolean Method in the String Class in Rails?

I have a method defined in string.rb, following the rules I've seen here on this site. 我有一个在string.rb中定义的方法,遵循在此站点上看到的规则。 It seems that the application sees the method because it doesn't error when it is called. 似乎应用程序可以看到该方法,因为调用该方法时不会出错。 It just seems to return true every time. 似乎每次都返回true。 I know, based on output data that it should be returning false. 我知道,基于输出数据,它应该返回false。 Help? 救命?

class String
    # Returns true or false if the string has useful data
  def useful?
    if self.nil?
        false
    elsif self.blank?
        false
    elsif self.empty?
        false
    elsif self == "unknown"
        false
    elsif self == "NA"
        false
    elsif self == "N/A"
        false
    else
        true
    end
  end
end

Based on your comments on the question, it seems the issue is rails recognizing the changes you made to the method, not problems with the method itself. 根据您对问题的评论,似乎问题是承认您对方法所做的更改很重要,而不是方法本身的问题。 You know that. 你懂的。 Here's a some more information on how rails reloads the source. 这是有关Rails如何重新加载源代码的更多信息。

When the rails server is running in the development environment it reloads itself when it detects that you have changed the code - so you don't have to restart the server in order to see the effect of changes you've made. 当Rails服务器在开发环境中运行时,它会在检测到您更改了代码时重新加载自身-因此您不必重新启动服务器即可查看所做更改的效果。 It checks if any files have been changed, clears the dependencies on this files, which are then reloaded as if you had just started the server. 它检查是否已更改任何文件,清除此文件上的依赖关系,然后像重新启动服务器一样重新加载它们。

I can't explain why it wouldn't reload your string.rb file automatically, though. 不过,我无法解释为什么它不会自动重新加载string.rb文件。

For your curiosity's sake, here is a very detailed blog post about how rails reloads your source code in development 为了您的好奇心,这是一篇非常详细的博客文章,内容涉及Rails如何在开发中重新加载源代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM