Class: RubyClasses::String::String

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

Instance Method Summary collapse

Instance Method Details

#append(str) ⇒ Object



110
111
112
# File 'lib/ruby_classes/strings.rb', line 110

def append(str)
  self+str.to_s
end

#asterisks(ch = '*') ⇒ Object

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



134
135
136
# File 'lib/ruby_classes/strings.rb', line 134

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

#basenameObject



8
9
10
# File 'lib/ruby_classes/strings.rb', line 8

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)



173
174
175
176
177
178
179
180
181
182
# File 'lib/ruby_classes/strings.rb', line 173

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



20
21
22
# File 'lib/ruby_classes/strings.rb', line 20

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

#depurate_for_fileObject



103
104
105
# File 'lib/ruby_classes/strings.rb', line 103

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

#double_quoteObject



101
# File 'lib/ruby_classes/strings.rb', line 101

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

#escape_double_quotesObject



51
52
53
# File 'lib/ruby_classes/strings.rb', line 51

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

#escape_printfObject



47
48
49
# File 'lib/ruby_classes/strings.rb', line 47

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

#escape_single_quotesObject



54
55
56
# File 'lib/ruby_classes/strings.rb', line 54

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

#flag(nation) ⇒ Object



92
93
94
# File 'lib/ruby_classes/strings.rb', line 92

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

#from_first_line_matching(regex_from) ⇒ Object

greppa dalla prima occorrenza di FROM in poi



200
201
202
203
204
205
206
207
208
209
210
211
# File 'lib/ruby_classes/strings.rb', line 200

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


80
81
82
83
84
85
86
87
88
# File 'lib/ruby_classes/strings.rb', line 80

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



164
# File 'lib/ruby_classes/strings.rb', line 164

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

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



12
13
14
15
16
17
18
# File 'lib/ruby_classes/strings.rb', line 12

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



38
39
40
# File 'lib/ruby_classes/strings.rb', line 38

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

#prepend(str) ⇒ Object



107
108
109
# File 'lib/ruby_classes/strings.rb', line 107

def prepend(str)
  str.to_s+self
end

#quote(sep = nil) ⇒ Object

enclose string in single quotes..



97
98
99
100
# File 'lib/ruby_classes/strings.rb', line 97

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

#sanitize_pathObject

sanitizza un filename



152
153
154
# File 'lib/ruby_classes/strings.rb', line 152

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

#shorten(count = 50) ⇒ Object



138
139
140
141
142
143
144
145
146
147
# File 'lib/ruby_classes/strings.rb', line 138

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 :-(



185
186
187
188
189
190
191
192
193
# File 'lib/ruby_classes/strings.rb', line 185

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

#starts_with?(ch) ⇒ Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/ruby_classes/strings.rb', line 129

def starts_with?(ch)
  self.match(/^#{ch}/)
end

#tee(opts = {}) ⇒ Object

dumps string to file (in APPEND) :)



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/ruby_classes/strings.rb', line 59

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



30
31
32
33
34
35
36
# File 'lib/ruby_classes/strings.rb', line 30

def to_class
	Object.const_get(self)
	#ret = eval(self)
	#deb "this is a class? cls=#{ret.class}"
	#raise "Exception its not a class!" if ret.class.to_s != 'Class'
	#return ret
end

#to_first_line_matching(regex_from) ⇒ Object



213
214
215
216
217
218
# File 'lib/ruby_classes/strings.rb', line 213

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 –>



117
118
119
120
121
122
123
124
125
126
127
# File 'lib/ruby_classes/strings.rb', line 117

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



160
161
162
# File 'lib/ruby_classes/strings.rb', line 160

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

#trimObject

rimuove spazi a inizio e fine



43
44
45
# File 'lib/ruby_classes/strings.rb', line 43

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

#uncolorObject



24
25
26
# File 'lib/ruby_classes/strings.rb', line 24

def uncolor()
  scolora(self)
end