简体   繁体   中英

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. 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. I know, based on output data that it should be returning 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.

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. 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.

For your curiosity's sake, here is a very detailed blog post about how rails reloads your source code in development

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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