Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/dragonfly_extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.random_alphanumeric(length = 6) ⇒ Object



163
164
165
166
167
168
# File 'lib/dragonfly_extensions.rb', line 163

def String.random_alphanumeric(length=6)
  chars  = ['A'..'Z','0'..'9'].map{|r|r.to_a}.flatten
  ignore = ['0','1','L','A','E','I','O','U']
  valid = (chars - ignore)
  Array.new(length).map{valid[rand(valid.size)]}.join.downcase
end

Instance Method Details

#asset_escapeObject



170
171
172
# File 'lib/dragonfly_extensions.rb', line 170

def asset_escape
  CGI::escape(self.downcase.downcase.gsub(/\s+/,'_').gsub(/[^[:alnum:]_]/, ''))
end

#capitalize_each_wordObject



126
127
128
# File 'lib/dragonfly_extensions.rb', line 126

def capitalize_each_word
  self.downcase.gsub(/^[a-z]|\s+[a-z]/) { |a| a.upcase }
end

#chunk_by_length(max_length = 40) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/dragonfly_extensions.rb', line 148

def chunk_by_length(max_length = 40)
  result = []
  words = self.split
  line = ''
  for word in words
    if (line.length > max_length) or (line.length + word.length > max_length)
      result << line
      line = ''
    end
    line << ' ' + word 
  end
  result << line
  result
end

#chunk_by_words(chunk_size = 8) ⇒ Object



139
140
141
142
143
144
145
146
# File 'lib/dragonfly_extensions.rb', line 139

def chunk_by_words(chunk_size = 8)
  result = []
  words = self.split
  for chunk in words.in_groups_of(chunk_size)
    result << chunk.join(' ')
  end
  result
end

#compressObject



130
131
132
# File 'lib/dragonfly_extensions.rb', line 130

def compress
  self.gsub(' ','')
end

#initial_capsObject

unicode_str.initial_caps => new_str returns a copy of a string with initial capitals “Jules-Édouard”.initial_caps => “J.É.”



122
123
124
# File 'lib/dragonfly_extensions.rb', line 122

def initial_caps
  self.tr('-', ' ').split(' ').map { |word| word.chars.first.upcase.to_s + "." }.join
end

#truncate_words(length = 10, end_string = ' …') ⇒ Object



134
135
136
137
# File 'lib/dragonfly_extensions.rb', line 134

def truncate_words(length = 10, end_string = '')
  words = self.split()
  words[0..(length-1)].join(' ') + (words.length > length ? end_string : '')
end