Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/cotcube-helpers/string_ext.rb,
lib/cotcube-helpers/swig/date.rb

Overview

Monkey patching the Ruby Core class String

Instance Method Summary collapse

Instance Method Details

#escape_regexObject



13
14
15
# File 'lib/cotcube-helpers/string_ext.rb', line 13

def escape_regex
  chars.map{|z| %w[ . | ( ) [ ] { } \ ^ $ + * ? ].include?(z) ? "\\#{z}" : z }.join
end

#to_dateObject



28
29
30
# File 'lib/cotcube-helpers/swig/date.rb', line 28

def to_date
  Date::strptime(self, "%Y-%m-%d")
end

#to_timeObject



32
33
34
# File 'lib/cotcube-helpers/swig/date.rb', line 32

def to_time
  DateTime::strptime(self, "%s")
end

#valid_json?Boolean Also known as: is_valid_json?

Returns:

  • (Boolean)


6
7
8
9
10
11
# File 'lib/cotcube-helpers/string_ext.rb', line 6

def valid_json?
  JSON.parse(self)
  true
rescue JSON::ParserError
  false
end