Class: String
Overview
String extensions.
Constant Summary collapse
- LINE_RE =
/^([^\r]*)\r\n/n.freeze
- EMPTY_STRING =
''.freeze
Instance Method Summary collapse
-
#/(o) ⇒ Object
Concatenates a path (purely sugar).
-
#camelize ⇒ Object
Converts an underscored name into a camelized name.
- #etag ⇒ Object
- #get_line ⇒ Object
- #get_up_to_boundary(boundary) ⇒ Object
- #get_up_to_boundary_with_crlf(boundary) ⇒ Object
- #html_escape ⇒ Object
-
#underscore ⇒ Object
Converts camel-cased phrases to underscored phrases.
-
#uri_escape ⇒ Object
Encodes a normal string to a URI string.
-
#uri_unescape ⇒ Object
Decodes a URI string to a normal string.
Instance Method Details
#/(o) ⇒ Object
Concatenates a path (purely sugar)
21 22 23 |
# File 'lib/serverside/core_ext.rb', line 21 def /(o) File.join(self, o.to_s) end |
#camelize ⇒ Object
Converts an underscored name into a camelized name
32 33 34 |
# File 'lib/serverside/core_ext.rb', line 32 def camelize gsub(/(^|_)(.)/) {$2.upcase} end |
#etag ⇒ Object
59 60 61 |
# File 'lib/serverside/core_ext.rb', line 59 def etag MD5.hexdigest(self) end |
#get_line ⇒ Object
39 40 41 |
# File 'lib/serverside/core_ext.rb', line 39 def get_line sub!(LINE_RE, EMPTY_STRING) ? $1 : nil end |
#get_up_to_boundary(boundary) ⇒ Object
43 44 45 46 47 48 49 |
# File 'lib/serverside/core_ext.rb', line 43 def get_up_to_boundary(boundary) if i = index(boundary) part = i > 0 ? self[0..(i - 1)] : '' slice!(0..(i + boundary.size - 1)) part end end |
#get_up_to_boundary_with_crlf(boundary) ⇒ Object
51 52 53 54 55 56 57 |
# File 'lib/serverside/core_ext.rb', line 51 def get_up_to_boundary_with_crlf(boundary) if i = index(boundary) part = i > 0 ? self[0..(i - 1)] : '' slice!(0..(i + boundary.size + 1)) part end end |
#html_escape ⇒ Object
16 17 18 |
# File 'lib/serverside/core_ext.rb', line 16 def html_escape gsub(/&/, "&").gsub(/\"/, """).gsub(/>/, ">").gsub(/</, "<") end |
#underscore ⇒ Object
Converts camel-cased phrases to underscored phrases.
26 27 28 29 |
# File 'lib/serverside/core_ext.rb', line 26 def underscore gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').gsub(/([a-z\d])([A-Z])/,'\1_\2'). tr("-", "_").downcase end |
#uri_escape ⇒ Object
Encodes a normal string to a URI string.
6 7 8 9 |
# File 'lib/serverside/core_ext.rb', line 6 def uri_escape gsub(/([^ a-zA-Z0-9_.-]+)/n) {'%'+$1.unpack('H2'*$1.size). join('%').upcase}.tr(' ', '+') end |
#uri_unescape ⇒ Object
Decodes a URI string to a normal string.
12 13 14 |
# File 'lib/serverside/core_ext.rb', line 12 def uri_unescape tr('+', ' ').gsub(/((?:%[0-9a-fA-F]{2})+)/n){[$1.delete('%')].pack('H*')} end |