Skip to content

Instantly share code, notes, and snippets.

@PatrickLef
Created February 22, 2011 15:45
Show Gist options
  • Save PatrickLef/838858 to your computer and use it in GitHub Desktop.
Save PatrickLef/838858 to your computer and use it in GitHub Desktop.
ruby_monkeypatch
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