Class: String

Inherits:
Object
  • Object
show all
Includes:
RicDebug
Defined in:
lib/ric_strings.rb,
lib/classes/strings.rb

Direct Known Subclasses

RicColorsObsolete::RicColor

Constant Summary collapse

RICVERSION =
'1.1'

Instance Method Summary collapse

Methods included from RicDebug

#to_verbose_s, #trace, #whoami?

Instance Method Details

#append(str) ⇒ Object



112
113
114
# File 'lib/ric_strings.rb', line 112

def append(str)
  self+str.to_s
end

#asterisks(ch = '*') ⇒ Object

‘abcde’ => ‘*****’



141
142
143
# File 'lib/ric_strings.rb', line 141

def asterisks(ch='*')
  ch * (self.size)
end

#basenameObject



6
7
8
# File 'lib/ric_strings.rb', line 6

def basename
  split('/').last
end

#canonicalizeObject

non funziona bene perche non so come CACCHIO mappare il /g. oare che in ruby ci sia solo /i x(xtended) n(multiline)



180
181
182
183
184
185
186
187
188
189
# File 'lib/ric_strings.rb', line 180

def canonicalize
  value = self
  value = value.gsub( /^\s+/,  '')  # s/^\s+//;
  value = value.gsub( /\s+$/,  '')  # value =~ s/\s+$//;
  value = value.gsub( /\r*\n/n, ' ')  # value =~ s/\r*\n/ /g;
  value = value.gsub( /\s+$/n , ' ')  # value =~ s/\s+$/ /g;
  deb "Original:  '''#{self}'''"
  deb "Canonical: '''#{value}'''"
  return value
end

#color(mycolor = :orange) ⇒ Object



18
19
20
# File 'lib/ric_strings.rb', line 18

def color(mycolor = :orange )
  send(mycolor,self)
end

#depurate_for_fileObject



105
106
107
# File 'lib/ric_strings.rb', line 105

def depurate_for_file 
  gsub(/[\/: \.]/ , '_')
end

#double_quoteObject



103
# File 'lib/ric_strings.rb', line 103

def double_quote(); quote('"') ; end

#escape_double_quotesObject



45
46
47
# File 'lib/ric_strings.rb', line 45

def escape_double_quotes
  gsub('"','\"')
end

#escape_printfObject



41
42
43
# File 'lib/ric_strings.rb', line 41

def escape_printf
   gsub("%",'%%')
end

#escape_single_quotesObject



48
49
50
# File 'lib/ric_strings.rb', line 48

def escape_single_quotes
  gsub("'","\'")
end

#flag(nation) ⇒ Object



94
95
96
# File 'lib/ric_strings.rb', line 94

def flag(nation)
  flag(self, flag = '')
end

#from_first_line_matching(regex_from) ⇒ Object

greppa dalla prima occorrenza di FROM in poi



211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/ric_strings.rb', line 211

def from_first_line_matching(regex_from)
  arr_lines = self.split("\n")
  ix1 = arr_lines.index_regex(regex_from) || 0
  if ! ix1
    throw "Cattivo Indice per arr_lines. Non greppa una fava con: #{regex_from}"
    ix1 = 0
  end
  ix2 = arr_lines.length
  deb "#{ix1}..#{ix2}"
  joint = arr_lines[ix1..ix2].join("\n") #rescue ''
  return joint 
end

#grep_color(regex, opts = {}) ⇒ Object

return string with regex highlighted

TODO highlight just the matching regex


74
75
76
77
78
79
80
81
82
# File 'lib/ric_strings.rb', line 74

def grep_color(regex, opts={} )
  deb "string grep_color(regex: '#{regex}')"
  color   = opts[:color]   || true # false
  verbose = opts[:verbose] || true # TODO false
  return red(self) if self.match(regex)
  return verbose ?
    self :
    ''
end

#initialObject



171
# File 'lib/ric_strings.rb', line 171

def initial;  self[0..0] ; end

#left(length, padding) ⇒ Object Also known as: right



10
11
12
13
14
15
16
# File 'lib/ric_strings.rb', line 10

def left(length,padding)
  mylen = self.length
  padding_char = padding[0] # troppo difficile fare che paddi "abc" su 8 fa "abcabcab" checcavolo :)
  mylen < length ?
    self + padding * (length - mylen) :
    self 
end

#nlinesObject



32
33
34
# File 'lib/ric_strings.rb', line 32

def nlines
  split("\n").size
end

#prepend(str) ⇒ Object



109
110
111
# File 'lib/ric_strings.rb', line 109

def prepend(str)
  str.to_s+self
end

#quote(sep = nil) ⇒ Object

enclose string in single quotes..



99
100
101
102
# File 'lib/ric_strings.rb', line 99

def quote(sep = nil)
  sep ||= "'"
  sep + self + sep
end

#sanitize_pathObject

sanitizza un filename



159
160
161
# File 'lib/ric_strings.rb', line 159

def sanitize_path
  gsub(/[\/: ]/,'_')
end

#shorten(count = 50) ⇒ Object



145
146
147
148
149
150
151
152
153
154
# File 'lib/ric_strings.rb', line 145

def shorten (count = 50)
  if self.length >= count
    shortened = self[0, count]
    splitted = shortened.split(/\s/)
    words = splitted.length
    splitted[0, words-1].join(" ") + ' ...'
  else
    self
  end
end

#singolarizzaObject

a manhouse :-(



196
197
198
199
200
201
202
203
204
# File 'lib/ric_strings.rb', line 196

def singolarizza
  return self.singularize if self.respond_to?(:singularize)
  deb("singularize '#{self}'")
  m = self.match( /(.*)es$/)  # i.e. matches
  return m[1] if (m)
  m = self.match( /(.*)s$/)
  return m[1] if m
  return m
end

#tee(opts = {}) ⇒ Object

dumps string to file (in APPEND) :)



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/ric_strings.rb', line 53

def tee(opts={})
  filename = opts.fetch :file, __FILE__ + ".tmpric"
  verbose  = opts.fetch :verbose,  true
  preamble = lambda {|where| return "# Teeing string (pid=#{$$}, size=#{self.size}B, opts=#{opts}) into file: #{blue filename}\n" }
  deb "Teeing string (#{self.size}B, opts=#{opts}) into file: #{blue filename}"
  File.open(filename, 'a') {|f|
    f.write("# {{{  String.tee #{} from #{__FILE__}\n") if verbose
    f.write(preamble.call(:BEGIN)) if verbose
    f.write(self)
    f.write("\n" + preamble.call(:END))if verbose
    f.write("\n# }}} Tee stop pid=##{$$}\n")if verbose
  } # i think it closes it here...
  puts "Written #{self.size}B in append to: '#{yellow filename}'" if verbose
  return self
end

#to_classObject



28
29
30
# File 'lib/ric_strings.rb', line 28

def to_class
	Object.const_get(self)
end

#to_first_line_matching(regex_from) ⇒ Object



224
225
226
227
228
229
# File 'lib/ric_strings.rb', line 224

def to_first_line_matching(regex_from)
  arr_lines = self.split("\n")
  ix2 = arr_lines.index_regex(regex_from)
  #ix2 = arr_lines.length
  return arr_lines[0..ix2].join("\n")
end

#to_hash(description = 'String::to_hash() made by genious RCarlesso (give a better description if u dont like this!)') ⇒ Object

a: b c: d –>



119
120
121
122
123
124
125
126
127
128
129
# File 'lib/ric_strings.rb', line 119

def to_hash(description='String::to_hash() made by genious RCarlesso (give a better description if u dont like this!)')
  arr = Hash.new
  arr['_meta']  = Hash.new
  arr['_meta']['description'] = description
  arr['_meta']['time'] = Time.now
  self.split("\n").each{|line| 
    k,v = line.split(': ') 
    arr[k.strip] = v.strip 
    } rescue arr['_meta']['errors'] = $! 
  arr
end

#to_htmlObject

see arrays! def method_missing end



167
168
169
# File 'lib/ric_strings.rb', line 167

def to_html
  %(<span class="ricsvn_string" >#{self}</span>)
end

#trimObject

rimuove spazi a inizio e fine



37
38
39
# File 'lib/ric_strings.rb', line 37

def trim
  self.gsub(/^\s+/,'').gsub(/\s+$/,'')
end

#uncolorObject



22
23
24
# File 'lib/ric_strings.rb', line 22

def uncolor()
  scolora(self)
end