Class: String

Inherits:
Object show all
Defined in:
lib/rubyhacks.rb,
lib/rubyhacks.rb

Constant Summary collapse

FORTRAN_BOOLS =
[".true.", ".false.", "T", "F", ".t.", ".f.", ".T.", ".F."]

Instance Method Summary collapse

Instance Method Details

#+(other) ⇒ Object



194
195
196
197
198
199
200
# File 'lib/rubyhacks.rb', line 194

def +(other)
	if other.class == Symbol
		old_plus(other.to_s)
	else
		old_plus(other)
	end
end

#-@Object



341
342
343
344
345
# File 'lib/rubyhacks.rb', line 341

def -@
    @sign ||= 1
    @sign *= -1
    self
end

#<=>(other) ⇒ Object



355
356
357
358
359
360
361
362
363
364
365
366
# File 'lib/rubyhacks.rb', line 355

def <=> other
	begin
		@sign ||= 1
		if @sign == 1 
			return self.old_compare other
		else
			return other.old_compare self
		end
	rescue RuntimeError
		return self.old_compare other
	end
end

#delete_substrings(*strings) ⇒ Object Also known as: delsubstr



376
377
378
379
380
381
382
# File 'lib/rubyhacks.rb', line 376

def delete_substrings(*strings)
	new_string = self
	strings.each do |str|
		new_string = new_string.sub(Regexp.new_escaped(str), '')
	end
	new_string
end

#esc_regexObject



368
369
370
# File 'lib/rubyhacks.rb', line 368

def esc_regex
	Regexp.new(Regexp.escape(self))
end

#fortran_false?Boolean

Returns:

  • (Boolean)


329
330
331
# File 'lib/rubyhacks.rb', line 329

def fortran_false?
	self =~ /^(F|\.F\.|\.false\.|\.f\.)$/
end

#fortran_true?Boolean

Returns:

  • (Boolean)


325
326
327
# File 'lib/rubyhacks.rb', line 325

def fortran_true?
	self =~ /^(T|\.T\.|\.true\.|\.t\.)$/
end

#get_signObject



346
347
348
349
350
351
352
# File 'lib/rubyhacks.rb', line 346

def get_sign
	if self[0,1] == '-'
		@sign = -1
		self.sub!(/^\-/, '')
	end
	self
end

#grep(pattern) ⇒ Object



260
261
262
# File 'lib/rubyhacks.rb', line 260

def grep(pattern)
	split("\n").grep(pattern).join("\n")
end

#grep_line_numbers(pattern) ⇒ Object



264
265
266
267
268
269
270
# File 'lib/rubyhacks.rb', line 264

def grep_line_numbers(pattern)
	ans = []
	split("\n").each_with_index do |line, index|
		ans.push index + 1 if line =~ pattern
	end
	ans
end

#old_compareObject



353
# File 'lib/rubyhacks.rb', line 353

alias :old_compare :<=>

#old_plusObject



193
# File 'lib/rubyhacks.rb', line 193

alias old_plus +

#one_page_at_a_timeObject



213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/rubyhacks.rb', line 213

def one_page_at_a_time
	rows, columns = Terminal.terminal_size
	i = 0
	array = self.split("\n")
# 		puts array.size; gets;
	loop do 
		j = 0
		while j < rows - 8
			line = array[i]
			puts line
			i+=1
			break if i == array.size
			j+=(line.size / columns).ceiling
#  				puts i,j; gets
		end
		break if i == array.size
		puts "\nPress Enter to continue reading"
		gets
		3.times{print "\033[1A\033[K"}
	end
end

terminal_size = [rows, cols]



272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/rubyhacks.rb', line 272

def print_coloured_lines(terminal_size = nil) # terminal_size = [rows, cols]
# 		raise CRFatal.new("terminal size must be given if this is called any where except inside a terminal (for example if you've called this as a subprocess)") unless terminal_size or $stdout.tty?
	terminal_size ||= Terminal.terminal_size
	#lots of gritty terminal jargon in here. Edit at your peril!
	lines = self.split($/)
	i = 0; j=0
	lines.each do |line|
		colour =  j%2==0 ? j%4==0 ? "\033[0;36m" : "\033[1;32m" : "\033[1;37m" 
		print colour
		print line.gsub(/\|/, "\033[1;37m" + '|' + colour) 
		print "\033[K\n"
# 				puts (line.size / Terminal.terminal_size[1]).class
# 				puts (line.size / Terminal.terminal_size[1])

		i+=(line.sub(/\w*$/, '').size / terminal_size[1]).ceiling
		j+=1
	end
	puts "\033[1;37m"
# 		print 'rewind size is', rewind

end

#rewind(offset = 0) ⇒ Object



242
243
244
245
246
# File 'lib/rubyhacks.rb', line 242

def rewind(offset=0)
	#clears a string from the terminal output and rewinds to its beginning (as long as it was the last thing sent to standard out)
# 		puts offset; gets
	(true_lines-offset).times{print "\033[A\033[K"}
end

#signObject



337
338
339
340
# File 'lib/rubyhacks.rb', line 337

def sign
	@sign ||= 1
	@sign
end

#to_boolObject Also known as: to_b



202
203
204
205
206
207
208
209
210
# File 'lib/rubyhacks.rb', line 202

def to_bool
	if self == "true" or self == "T" or self == "TRUE"
		return true
	elsif self == "false" or self == "F" or self == "FALSE"
		return false
	else 
		raise ArgumentError.new("Attempt to convert string: (#{self}) to boolean failed")
	end
end

#to_fObject



256
257
258
# File 'lib/rubyhacks.rb', line 256

def to_f
	sub(/\.[eE]/, '.0e').old_to_f
end

#to_f_wspObject



662
663
664
# File 'lib/rubyhacks.rb', line 662

def to_f_wsp
 self.sub(/^\s*\-\s*/, '-').to_f
end

#to_hObject

Raises:

  • (ArgumentError)


235
236
237
238
239
240
# File 'lib/rubyhacks.rb', line 235

def to_h
	hash = eval(self)
	raise ArgumentError.new("Couldn't convert #{self} to hash") unless hash.class == Hash
# 		puts hash; gets
	return hash	
end

#true_linesObject



249
250
251
252
253
# File 'lib/rubyhacks.rb', line 249

def true_lines
	return split("\n", -1).inject(0) do |j, line|
		j + (line.size / Terminal.terminal_size[1]).ceiling
	end
end

#universal_escape(name = nil) ⇒ Object

Raises:

  • (ArgumentError)


294
295
296
297
298
299
300
301
302
303
# File 'lib/rubyhacks.rb', line 294

def universal_escape(name = nil)
	raise ArgumentError if name unless name =~ /^[A-Za-z0-9]*$/
	arr = []
	self.each_codepoint{|cp| arr.push cp}
	if name
		return "UEBZXQF#{name}UEB#{arr.join('d')}UEEZXQF#{name}UEE"
	else 
		return arr.join('d')
	end
end

#universal_escape1Object



304
305
306
307
308
# File 'lib/rubyhacks.rb', line 304

def universal_escape1
	arr = []
	self.each_byte{|cp| arr.push cp}
	arr.join('d')
end

#universal_unescape(name = nil) ⇒ Object



310
311
312
313
314
315
316
317
318
319
320
321
322
323
# File 'lib/rubyhacks.rb', line 310

def universal_unescape(name = nil)
# 		p arrxy = self.split('d').map{
# 		strxy = ""
# 		arrxy.each{|codepointx| print codepointx.to_i.pretty_inspect, ' '; strxy << codepointx.to_i}
# 		return strxy
	if name
		return self.gsub(Regexp.new("UEBZXQF#{name}UEB([\\dd]+)UEEZXQF#{name}UEE")) do
			$1.universal_unescape
		end
	else
		raise TypeError.new("This string is not a simple escaped string: #{self}") unless self =~ /^[\dd]*$/	
		self.sub(/^UEB.*UEB/, '').sub(/UEE.*UEE$/, '').split('d').map{|cp| cp.to_i}.pack('U*')
	end
end

#variable_to_class_nameObject



372
373
374
# File 'lib/rubyhacks.rb', line 372

def variable_to_class_name
	self.gsub(/(?:^|_)(\w)/){"#$1".capitalize}
end