Class: String

Inherits:
Object show all
Defined in:
lib/rext/string/encode.rb,
lib/rext/string/helpers.rb

Defined Under Namespace

Classes: InvalidSwitchError

Instance Method Summary collapse

Instance Method Details

#base64_decodeObject

Return Base 64 decoded string.

Examples

'Y29va2llcw=='.base64_decode  # => cookies


40
41
42
# File 'lib/rext/string/encode.rb', line 40

def base64_decode
  unpack('m').first
end

#base64_encodeObject

Return Base 64 encoded string.

Examples

'cookies'.base64_encode  # => Y29va2llcw==


52
53
54
# File 'lib/rext/string/encode.rb', line 52

def base64_encode
  [self].pack('m').chop
end

#camelize(capitalize_first_letter = false) ⇒ Object

Convert a string to camel-case, and optionally capitalize_first_letter.



45
46
47
48
49
50
# File 'lib/rext/string/helpers.rb', line 45

def camelize capitalize_first_letter = false
  string = Extlib::Inflection.camelize(self)
  return string if capitalize_first_letter
  string[0,1] = string.first.downcase
  string
end

#constantizeObject

Returns a constant when the string is a valid constant name.



38
39
40
# File 'lib/rext/string/helpers.rb', line 38

def constantize
  Extlib::Inflection.constantize self
end

#dasherizeObject

Replace all underscores with hyphens.



158
159
160
# File 'lib/rext/string/helpers.rb', line 158

def dasherize
  tr '_', '-'
end

#digitizeObject

Digitize a string.

Examples:

'$100,000'.digitize  # => 100000


31
32
33
# File 'lib/rext/string/helpers.rb', line 31

def digitize
  gsub /[^\d]/, ''
end

#ends_with?(string) ⇒ Boolean

Check if a string ends with another string.

Examples

'foo bar'.ends_with? 'bar'  # => true

Returns:

  • (Boolean)


87
88
89
# File 'lib/rext/string/helpers.rb', line 87

def ends_with? string
  rindex(string) == length - string.length
end

#escape_htmlObject

Escape html entities. Shortcut for Rack::Utils.escape_html.

Examples

'im <strong>strong</strong>.escape_html  # => im &lt;strong&gt;strong&lt;/strong&gt;


78
79
80
# File 'lib/rext/string/encode.rb', line 78

def escape_html
  Rack::Utils.escape_html self
end

#first(n = 1) ⇒ Object

First n character(s).

Examples

'foo'.first     # => f
'foo'.first(2)  # => fo


126
127
128
# File 'lib/rext/string/helpers.rb', line 126

def first n = 1
  self[0, n]
end

#frequency_of_word(word) ⇒ Object

Return frequency of word or 0.

Examples

'foo foo bar'.frequency_of_word('foo') # => 2
'foo foo bar'.frequency_of_word('bar') # => 1


194
195
196
# File 'lib/rext/string/helpers.rb', line 194

def frequency_of_word word
  word_frequency[word]
end

#from(n) ⇒ Object

All characters after n.

Examples

'.css'.from(1)  #=> css


151
152
153
# File 'lib/rext/string/helpers.rb', line 151

def from n
  self[n, length]
end

#last(n = 1) ⇒ Object

Last n character(s).

Examples

'bar'.last     # => r
'bar'.last(2)  # => ar


139
140
141
# File 'lib/rext/string/helpers.rb', line 139

def last n = 1
  self[-n, n]
end

#merge_word(word) ⇒ Object Also known as: add_class

Merge the word passed into the string. This is useful for adding classes which may already exist in a string.

Examples

'product'.merge_word('sold')        # => "product sold"
'product sold'.merge_word('sold')   # => "product sold"


17
18
19
20
# File 'lib/rext/string/helpers.rb', line 17

def merge_word word
  self << " #{word}" unless split.include? word
  self
end

#plural?Boolean

Determines if a string is plural.

Examples

'cookies'.plural?  # => true
'cookie'.plural?   # => false

Returns:

  • (Boolean)


100
101
102
# File 'lib/rext/string/helpers.rb', line 100

def plural?
  singular != self
end

#remove(regexp) ⇒ Object

Shortcut for #gsub!(regexp, ”)



165
166
167
# File 'lib/rext/string/helpers.rb', line 165

def remove regexp
  gsub! regexp, ''
end

#singular?Boolean

Determines if a string is singular.

Examples

'cookies'.singular?  # => false
'cookie'.singular?   # => true

Returns:

  • (Boolean)


113
114
115
# File 'lib/rext/string/helpers.rb', line 113

def singular?
  not plural?
end

#starts_with?(string) ⇒ Boolean

Check if a string starts with another string.

Examples

'foo bar'.starts_with? 'foo'  # => true

Returns:

  • (Boolean)


75
76
77
# File 'lib/rext/string/helpers.rb', line 75

def starts_with? string
  index(string) == 0
end

#switchifyObject

Returns the switch equivilant of this string.

Examples

'foo_bar'.switchify         # => --foo-bar
'lots_of_foobar'.switchify  # => --lots-of-foobar
't'.switchify               # => -t
''.switchify                # => InvalidSwitchError

Raises:



211
212
213
214
# File 'lib/rext/string/helpers.rb', line 211

def switchify
  raise InvalidSwitchError, 'switch must have a length > 0' if length.zero?
  length > 1 ? "--#{dasherize}" : "-#{self}"
end

#to_md5Object

Return 32 character md5 string.

Examples

'test'.to_md5  # => 098f6bcd4621d373cade4e832627b4f6


16
17
18
# File 'lib/rext/string/encode.rb', line 16

def to_md5
  ::Digest::MD5.hexdigest self
end

#to_sha512Object

Return 128 character sha512 string.

Examples

'test'.to_sha512  # => ee26b0dd4af7e749aa1a8ee3c10ae9923f618980772e473f8819a5d4940e0db27ac185f8a0e1d5f84f88bc887fd67b143732c304cc5fa9ad8e6f57f50028a8ff


28
29
30
# File 'lib/rext/string/encode.rb', line 28

def to_sha512
  ::Digest::SHA512.hexdigest self
end

#url_decodeObject

URL decode. Shortcut for Rack::Utils.unescape.



66
67
68
# File 'lib/rext/string/encode.rb', line 66

def url_decode
  Rack::Utils.unescape self
end

#url_encodeObject

URL encode. Shortcut for Rack::Utils.encode.



59
60
61
# File 'lib/rext/string/encode.rb', line 59

def url_encode
  Rack::Utils.escape self
end

#word_frequencyObject

Return hash of word frequencies.

Examples

'foo foo bar'.word_frequency 
# => { 'foo' => 2, 'bar' => 1 }


178
179
180
181
182
183
# File 'lib/rext/string/helpers.rb', line 178

def word_frequency
  split.inject Hash.new(0) do |frequencies, word|
    frequencies[word] += 1
    frequencies
  end
end

#wrap(prefix, suffix = nil) ⇒ Object

Wrap a string with a prefix and optional suffix. When the suffix is not present the prefix will be used.

Examples

'foo'.wrap('|')       # => |foo|
'foo'.wrap('(', ')')  # => (foo)


63
64
65
# File 'lib/rext/string/helpers.rb', line 63

def wrap prefix, suffix = nil
  prefix + self + (suffix || prefix)
end