Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/mark_facets/ruby/string.rb

Constant Summary collapse

RANDOM_CHARS =
[('A'..'H').to_a, ('J'..'N').to_a, ('P'..'T').to_a, ('W'..'Z').to_a, ('3'..'9').to_a].flatten

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.cast_from(object) ⇒ Object



127
128
129
# File 'lib/mark_facets/ruby/string.rb', line 127

def cast_from(object)
  object.to_s
end

.random(size = 20) ⇒ Object



121
122
123
124
125
# File 'lib/mark_facets/ruby/string.rb', line 121

def random(size = 20)
  newpass = ''
  1.upto(size) { |i| newpass << RANDOM_CHARS[rand(RANDOM_CHARS.size-1)] }
  return newpass.upcase
end

Instance Method Details

#hexdigestObject



8
9
10
# File 'lib/mark_facets/ruby/string.rb', line 8

def hexdigest
  Digest::SHA1.hexdigest(self)
end

#hexdigest!Object



12
13
14
# File 'lib/mark_facets/ruby/string.rb', line 12

def hexdigest!
  self.replace(self.hexdigest)
end

#md5_hashObject



4
5
6
# File 'lib/mark_facets/ruby/string.rb', line 4

def md5_hash
  Digest::MD5.hexdigest(self).to_s
end

#methodizeObject

Raises:

  • (NameError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/mark_facets/ruby/string.rb', line 26

def methodize
  m = self.dup
  m.strip!
  m.match(/([\?\=\!])$/)
  punc = $1
  m = m.underscore if m.respond_to?(:underscore)
  m.downcase!
  m = m[0..100] if m.length > 100
  m.gsub!(/(\W)/, '_')
  m.gsub!(/_+$/, '')
  m.gsub!(/^[^a-z]+/, '')
  [' ', '_', '?', '!', "="].each do |c|
    m.squeeze!(c)
  end
  raise NameError.new("#{self} cannot be converted to a valid method name!") if m.nil? || m == ''
  return "#{m}#{punc}"
end

#to_boolObject Also known as: to_boolean



16
17
18
19
20
21
22
# File 'lib/mark_facets/ruby/string.rb', line 16

def to_bool
  case self.downcase
  when 'n', 'no', 'f', 'false', 'nay'
    return false
  end
  return true
end