class String
Constants
- FORTRAN_BOOLS
Public Instance Methods
+(other)
click to toggle source
# File lib/rubyhacks.rb, line 194 def +(other) if other.class == Symbol old_plus(other.to_s) else old_plus(other) end end
Also aliased as: old_plus
-@()
click to toggle source
# File lib/rubyhacks.rb, line 341 def -@ @sign ||= 1 @sign *= -1 self end
<=>(other)
click to toggle source
# 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
Also aliased as: old_compare
delete_substrings(*strings)
click to toggle source
# 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
Also aliased as: delsubstr
esc_regex()
click to toggle source
# File lib/rubyhacks.rb, line 368 def esc_regex Regexp.new(Regexp.escape(self)) end
fortran_false?()
click to toggle source
# File lib/rubyhacks.rb, line 329 def fortran_false? self =~ /^(F|\.F\.|\.false\.|\.f\.)$/ end
fortran_true?()
click to toggle source
# File lib/rubyhacks.rb, line 325 def fortran_true? self =~ /^(T|\.T\.|\.true\.|\.t\.)$/ end
get_sign()
click to toggle source
# File lib/rubyhacks.rb, line 346 def get_sign if self[0,1] == '-' @sign = -1 self.sub!(/^\-/, '') end self end
grep(pattern)
click to toggle source
# File lib/rubyhacks.rb, line 260 def grep(pattern) split("\n").grep(pattern).join("\n") end
grep_line_numbers(pattern)
click to toggle source
# 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
one_page_at_a_time()
click to toggle source
# 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
paginate(command = "less")
click to toggle source
# File lib/rubyhacks.rb, line 385 def paginate(command = "less") system({"PAGINATE_STRING" => self.gsub(/\n/, "\\n").gsub(/\t/, "\\t")}, "echo $PAGINATE_STRING | #{command}") end
print_coloured_lines(terminal_size = nil)
click to toggle source
# 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)
click to toggle source
# 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
sign()
click to toggle source
# File lib/rubyhacks.rb, line 337 def sign @sign ||= 1 @sign end
to_bool()
click to toggle source
# 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
Also aliased as: to_b
to_f()
click to toggle source
# File lib/rubyhacks.rb, line 256 def to_f sub(/\.[eE]/, '.0e').old_to_f end
to_f_wsp()
click to toggle source
# File lib/rubyhacks.rb, line 673 def to_f_wsp self.sub(/^\s*\-\s*/, '-').to_f end
to_h()
click to toggle source
# 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_lines()
click to toggle source
# 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)
click to toggle source
# 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_escape1()
click to toggle source
# 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)
click to toggle source
# 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_name()
click to toggle source
# File lib/rubyhacks.rb, line 372 def variable_to_class_name self.gsub(/(?:^|_)(\w)/){"#$1".capitalize} end