Class: String

Inherits:
Object
  • Object
show all
Defined in:
lib/furik/core_ext/string.rb

Instance Method Summary collapse

Instance Method Details

#cut(size = 50, options = {}) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/furik/core_ext/string.rb', line 14

def cut(size = 50, options = {})
  text = self.dup
  options[:omission] ||= '...'
  chars = ''
  current_size = 0
  text.each_char do |c|
    current_size += c =~ /^[ -~。-゚]*$/ ? 1 : 2
    break if current_size > size
    chars << c
  end
  chars << options[:omission] if current_size > size
  chars.to_s
end

#plainObject



10
11
12
# File 'lib/furik/core_ext/string.rb', line 10

def plain
  self.gsub("\r\n", ' ').gsub(/[\s\-_=]{2,}/, ' ').strip
end

#underscoreObject



2
3
4
5
6
7
8
# File 'lib/furik/core_ext/string.rb', line 2

def underscore
  self.gsub(/::/, '/').
    gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
    gsub(/([a-z\d])([A-Z])/,'\1_\2').
    tr('-', '_').
    downcase
end