Class: String

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

Instance Method Summary collapse

Instance Method Details

#camel_caseObject



27
28
29
30
# File 'lib/origen/core_ext/string.rb', line 27

def camel_case
  Origen.deprecate "String#camel_case! is deprecated, use String#camelcase instead, or if you want to get rid of spaces: my_string.gsub(' ', '_').camelcase"
  gsub(/\s+/, '_').split('_').map(&:capitalize).join
end

#escape_underscoresObject Also known as: escape_underscore



22
23
24
# File 'lib/origen/core_ext/string.rb', line 22

def escape_underscores
  gsub('_', '\_')
end

#is_numeric?Boolean Also known as: numeric?

Check if a String is a numeric

Returns:

  • (Boolean)


93
94
95
96
# File 'lib/origen/core_ext/string.rb', line 93

def is_numeric?
  return true if self =~ /\A\d+\Z/
  true if Float(self) rescue false # rubocop:disable Style/RescueModifier
end

#pad_leading_zeros(width) ⇒ Object



32
33
34
35
36
# File 'lib/origen/core_ext/string.rb', line 32

def pad_leading_zeros(width)
  str = self
  (0..(width - size) - 1).each { str = '0' + str }
  str
end

#squeeze_linesObject



77
78
79
# File 'lib/origen/core_ext/string.rb', line 77

def squeeze_lines
  split(/\n+/).join(' ').squeeze(' ')
end

#symbolizeObject

Sanitizes the string for conversion to a symbol and returns a lower cased symbol version of the string



57
58
59
# File 'lib/origen/core_ext/string.rb', line 57

def symbolize
  gsub(/(\n|\s|\(|\)|\.|\[|\]|-|{|})/, '_').downcase.to_sym
end

#titleize(options = {}) ⇒ Object

Capitalize every word



114
115
116
117
118
119
120
121
122
123
# File 'lib/origen/core_ext/string.rb', line 114

def titleize(options = {})
  options = {
    keep_specials: false
  }.update(options)
  if options[:keep_specials]
    split.map(&:capitalize).join(' ')
  else
    split(/ |\_|\-/).map(&:capitalize).join(' ')
  end
end

#to_boolObject

Attempt to convert a String to a boolean answer



82
83
84
85
86
87
88
89
90
# File 'lib/origen/core_ext/string.rb', line 82

def to_bool
  if self == true || self =~ (/^(true|t|yes|y|1)$/i)
    return true
  elsif self == false || self.empty? || self =~ (/^(false|f|no|n|0)$/i)
    return false
  else
    return nil
  end
end

#to_decObject



12
13
14
15
16
17
18
19
20
# File 'lib/origen/core_ext/string.rb', line 12

def to_dec
  if self =~ /0x(.*)/
    Regexp.last_match[1].to_i(16)
  elsif self =~ /0b(.*)/
    Regexp.last_match[1].to_i(2)
  else
    to_i
  end
end

#to_lines(length) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/origen/core_ext/string.rb', line 38

def to_lines(length)
  lines = []
  line = []
  len = 0
  split(/\s+/).each do |word|
    if (len + word.length) > length
      lines << line.join(' ')
      line = []
      len = 0
    end
    line << word
    len += word.length + 1 # For the trailing space
  end
  lines << line.join(' ') unless line.empty?
  lines
end

#to_numericObject Also known as: to_number

Convert the String to a Numeric (Float or Integer)



100
101
102
103
104
105
106
107
108
109
110
# File 'lib/origen/core_ext/string.rb', line 100

def to_numeric
  if self.numeric?
    if to_i == to_f
      to_i
    else
      to_f
    end
  else
    fail "'#{self}' cannot be converted to a Numeric, exiting..."
  end
end

#to_snakecaseObject Also known as: snakecase



71
72
73
74
# File 'lib/origen/core_ext/string.rb', line 71

def to_snakecase
  Origen.deprecate 'String#to_snakecase is deprecated, use String#underscore instead since it is aware of FSL acronyms'
  dup.tap(&:to_snakecase!)
end

#to_snakecase!Object Also known as: snakecase!

acronyms



62
63
64
65
66
67
68
# File 'lib/origen/core_ext/string.rb', line 62

def to_snakecase!
  Origen.deprecate 'String#to_snakecase! is deprecated, use String#underscore instead since it is aware of FSL acronyms'
  gsub!(/\s+/, '')
  g = gsub!(/(.)([A-Z])/, '\1_\2');
  d = downcase!
  g || d
end