Created
February 22, 2011 15:45
-
-
Save PatrickLef/838858 to your computer and use it in GitHub Desktop.
ruby_monkeypatch
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Enumerable | |
# Convenient method instead of checking a.nil? || a.empty? | |
# Example: | |
# nil.emptil? ==> true | |
# [].emptil? ==> true | |
# {}.emptil? ==> true | |
# {:foo => 'bar'}.emptil? ==> false | |
# [1,2,3].emptil? ==> false | |
def emptil? | |
self.respond_to?('empty?') && self.empty? | |
end | |
# Checks if string is valid xml | |
def is_xml? | |
require 'nokogiri' | |
begin | |
bad_doc = Nokogiri::XML(self) { |config| config.strict } | |
rescue Nokogiri::XML::SyntaxError => e | |
return false | |
end | |
return true | |
end | |
# Checks if string is valid json | |
def is_json? | |
begin | |
JSON.parse(self) | |
rescue Exception => e | |
return false | |
end | |
return true | |
end | |
# Checks if string is valid marshal | |
def is_marshal? | |
begin | |
Marshal.load(self) | |
rescue Exception => e | |
return false | |
end | |
return true | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment