Class: String

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

Instance Method Summary collapse

Instance Method Details

#cap_decimals(num_decimals) ⇒ Object



18
19
20
21
# File 'lib/finmodeling/string_helpers.rb', line 18

def cap_decimals(num_decimals)
  r = Regexp.new('(.*\.[0-9]{' + num_decimals.to_s + '})[0-9]*')
  self.gsub(r, '\1')
end

#fixed_width_left_justify(width) ⇒ Object



2
3
4
5
6
# File 'lib/finmodeling/string_helpers.rb', line 2

def fixed_width_left_justify(width)
  return self[0..(width-1  )]       if self.length == width
  return self[0..(width-1-3)]+"..." if self.length > width
  return self + (" " * (width - self.length))
end

#fixed_width_right_justify(width) ⇒ Object



8
9
10
11
12
# File 'lib/finmodeling/string_helpers.rb', line 8

def fixed_width_right_justify(width)
  return       self[(-width  )..-1] if self.length == width
  return "..."+self[(-width+3)..-1] if self.length > width
  return (" " * (width - self.length)) + self
end

#matches_any_regex?(regexes) ⇒ Boolean

Returns:

  • (Boolean)


23
24
25
# File 'lib/finmodeling/string_helpers.rb', line 23

def matches_any_regex?(regexes)
  return regexes.inject(false){ |matches, regex| matches or regex =~ self }
end

#split_into_lines_shorter_than(max_line_width) ⇒ Object



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

def split_into_lines_shorter_than(max_line_width)
  lines = []
  cur_line = []

  split(' ').each do |word|
    if (cur_line + [word]).join(' ').length > max_line_width
      lines << cur_line.join(' ')
      cur_line = []
    end

    cur_line << word
  end

  lines << cur_line.join(' ') if !cur_line.empty?
  lines
end

#with_thousands_separatorsObject



14
15
16
# File 'lib/finmodeling/string_helpers.rb', line 14

def with_thousands_separators
  self.reverse.scan(/(?:\d*\.)?\d{1,3}-?/).join(',').reverse
end