Module: Escape::StringEscape

Defined in:
lib/repla/lib/escape.rb

Overview

Escaping strings

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.javascript_escape(string) ⇒ Object

TODO: self.javascript_escape and self.shell_escape should be private, but instance methods can’t call private class methods in Ruby? Or can they this only fails when the code imported into the gem?



54
55
56
57
58
59
60
61
# File 'lib/repla/lib/escape.rb', line 54

def self.javascript_escape(string)
  # string.gsub('\\', '\\\\\\\\').gsub("\n", '\\\\n').gsub("'", "\\\\'")
  # Combined as one command, comparable in speed:
  string.gsub(/\\\\|\n|'/,
              '\\\\' => '\\\\\\\\',
              "\n" => '\\n',
              "'" => '\\\'')
end

.shell_escape(string) ⇒ Object



63
64
65
# File 'lib/repla/lib/escape.rb', line 63

def self.shell_escape(string)
  Shellwords.escape(string)
end

Instance Method Details

#float?Boolean

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/repla/lib/escape.rb', line 32

def float?
  true if Float(self)
rescue StandardError
  false
end

#integer?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/repla/lib/escape.rb', line 38

def integer?
  to_i.to_s == self
end

#javascript_argumentObject



20
21
22
# File 'lib/repla/lib/escape.rb', line 20

def javascript_argument
  "'#{StringEscape.javascript_escape(self)}'"
end

#javascript_escapeObject



24
25
26
# File 'lib/repla/lib/escape.rb', line 24

def javascript_escape
  StringEscape.javascript_escape(self)
end

#javascript_escape!Object



28
29
30
# File 'lib/repla/lib/escape.rb', line 28

def javascript_escape!
  replace(StringEscape.javascript_escape(self))
end

#shell_escapeObject



42
43
44
# File 'lib/repla/lib/escape.rb', line 42

def shell_escape
  StringEscape.shell_escape(self)
end

#shell_escape!Object



46
47
48
# File 'lib/repla/lib/escape.rb', line 46

def shell_escape!
  replace(StringEscape.shell_escape(self))
end