Module: Useful::RubyExtensions::String::ClassMethods

Defined in:
lib/useful/ruby_extensions/string.rb

Constant Summary collapse

CGI_ESCAPE_MAP =

does some CGI escaping on a string

{
  '+'    => '%20'
}.freeze

Instance Method Summary collapse

Instance Method Details

#cgi_escape(string) ⇒ Object



22
23
24
25
# File 'lib/useful/ruby_extensions/string.rb', line 22

def cgi_escape(string)
  require 'cgi' unless defined?(::CGI) && defined?(::CGI::escape)
  ::CGI.escape(string).gsub(/(\+)/) { CGI_ESCAPE_MAP[$1] }
end

#hsub(string, hash) ⇒ Object

adds the contents of a2 to a1, removing duplicates



13
14
15
16
# File 'lib/useful/ruby_extensions/string.rb', line 13

def hsub(string, hash)
  hash.each {|k,v| string.gsub!(":#{k}",v.to_s)}
  string
end

#match?(string, pattern) ⇒ Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/useful/ruby_extensions/string.rb', line 27

def match?(string, pattern)
  !string.match(pattern).nil?
end

#show_regexp(string, re) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/useful/ruby_extensions/string.rb', line 35

def show_regexp(string, re)
  if string =~ re
    "#{$`}<<#{$&}>>#{$'}"
  else
    "no match"
  end
end

#valid_email?(string) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
# File 'lib/useful/ruby_extensions/string.rb', line 31

def valid_email?(string)
  match?(string, EMAIL_REGEXP)
end