Created
February 9, 2025 22:50
-
-
Save thomasjslone/e9514c75cbee059bd57a80463548a950 to your computer and use it in GitHub Desktop.
definitions is upto date, no old ass code, no pointless methods, dir image finnaly a thing that makes sense, class / object / self methods less redundant
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ALPHA= ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] | |
ALPHANUM = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","0","1","2","3","4","5","6","7","8","9"] | |
CHARS = [] ; c = 0 ; 256.times{ CHARS << c.chr.to_s ; c += 1 } | |
BINARY = [] ; c = 0 ; 256.times { b = c.to_s(2) ; until b.to_s.length == 8 ; b = "0" + b.to_s ; end ; BINARY << b ; c += 1 } | |
HEX = [] ; c = 0 ; 256.times { h = c.to_s(16) ; if h.length == 1 ; h = "0" + h.to_s ; end ; HEX << h ; c += 1 } | |
DAYS = ["sunday","monday","tuesday","wednesday","thursday","friday","saturday"] | |
MONTHS = ["january","february","march","april","may","june","july","august","september","october","november","december"] | |
SEASONS = ["spring","summer","autum","winter"] | |
KEYWORDS = ["alias", "and", "begin", "break", "case", "class", "def", "defined?", "do", "else", "elsif", "end", "ensure", | |
"false", "for", "if", "in", "module", "next", "nil", "not", "or", "redo", "rescue", "retry" "return", "self", | |
"super", "then", "true", "undef", "unless" "until", "when", "while", "yield", "loop"] | |
OPERATORS = ["+", "-", "*" ,"/", "%", "**", "==", "!=", ">", ">=", "<", "<=", "<=>", "===", ".eql?", "equal?", "!~", | |
"=~", "&", "|", "^", "~", "&&", "||"] | |
DATATYPES = ["Numeric", "Integer", "Float", "BigDecimal", "Rational", "Complex", "String", "Symbol", "Array", "Hash", | |
"Range", "Regexp", "Time", "Date", "DateTime", "TrueClass", "FalseClass", "NilClass"] | |
$ALPHA = ALPHA | |
$ALPHANUM = ALPHANUM | |
$CHARS = CHARS | |
$BINARY = BINARY | |
$HEX = HEX | |
$DAYS = DAYS | |
$MONTHS = MONTHS | |
$SEASONS = SEASONS | |
$KEYWORDS = KEYWORDS | |
$OPERATORS = OPERATORS | |
$DATATYPES = DATATYPES | |
def benchmarked_load(script) | |
if File.file?(script) == false ; raise "No such file."; end | |
begin ; t1 = Time.now ; load(script) ; t2=Time.now ; return t2-t1 | |
rescue LoadError, SyntaxError, SystemExit, Exception => e ; ; raise e.class.to_s + ": " + e.msg | |
end | |
end | |
def benchmarked_eval(script) | |
if script.to_s.length == 0 ; raise "Invalid arugment, cannot be nilstring." ; end | |
begin ; t1 = Time.now ; eval(script) ; t2=Time.now ; return t2-t1 | |
rescue LoadError, SyntaxError, SystemExit, Exception => e ; raise e.class.to_s + ": " + e.msg | |
end | |
end | |
Object.class_eval{ | |
def local_methods ; ms = self.methods ; mets = [] ; ms.each { |m| mets << m.to_s } ; rm = self.class.methods ; self.class.class.methods.each { |m| rm << m.to_s } ; nm = [] ; mets.each { |m| unless rm.include?(m.to_s) ; nm << m.to_s ; end } ; return nm ; end | |
alias :m :methods ; alias :lm :local_methods | |
alias :lv :local_variables ; alias :gv :global_variables ; alias :iv :instance_variables | |
alias :ivs :instance_variable_set ; alias :ivg :instance_variable_get ##dont forget get/set constants and classvariables | |
alias :iev :instance_eval ; alias :ev :eval | |
def constants ; self.class.constants ; end ; alias :cn :constants | |
def gets_eval *args ##multiline console input terminated with {end} | |
lines = [] ; if args.length==1; context = args[0] ; else; context = self ; end | |
loop do ; line = gets.chomp ; if line.to_s == "{end}"; break ; else; lines << line.to_s ; end ; end | |
code = lines.join("\n")+"\n" | |
begin; res = cont.instance_eval(code) ; rescue LoadError, SyntaxError, SystemExit, Exception => e ; if e.to_s == "exit" ; exit ; end ; res = "Error: " + e.class.to_s + ": " + e.msg ; end | |
return res | |
end | |
def _and(a,b) ; if a == 1 and b == 1 ; return 1 ; else ; return 0 ; end ; end | |
def _or(a,b) ; if a == 0 and b == 0 ; return 0 ; elsif a == 1 and b == 1 ; return 1 ; else ; return 1 ; end ; end | |
def _not(a,b) ; if a == 0 and b == 0 ; return 1 ; else ; return 0 ; end ; end | |
def _nor(a,b) ; if a == 0 and b == 0 ; return 1 ; else ; return 0 ; end ; end | |
def _nand(a,b) ; if a == 1 and b == 1 ; return 0 ; else ; return 1 ; end ; end | |
def _xor(a,b) ; if a == 0 and b == 0 ; return 0 ; elsif a == 1 and b == 1 ; return 0 ; else ; return 1 ; end ; end | |
def rands(length, nonumbers=false) | |
if length.to_i < 1; raise "Invalid argument."; end | |
str = [] | |
if nonumbers == false; length.times { str<<($ALPHANUM.shuffle)[0] } | |
else; length.times { str<<($ALPHA.shuffle)[0] } | |
end | |
return str.join('') | |
end | |
} | |
Dir.instance_eval{ | |
def dir *args | |
if args.length==0;return Dir.getwd.to_s | |
elsif args[0].is_a?(String) | |
if File.directory?(args[0]) ; Dir.chdir(args[0]) ; return Dir.getwd.to_s | |
elsif File.directory?(Dir.getwd.to_s + "/" + args[0].to_s) ; Dir.chdir(Dir.getwd.to_s + "/" + args[0].to_s) ; return Dir.getwd.to_s | |
else ; raise "No such directory." | |
end | |
end | |
end | |
def view *args | |
if args.length == 0 ; dir = Dir.getwd.to_s | |
elsif File.directory?(args[0].to_s) ; dir = args[0].to_s | |
elsif File.directory?(Dir.getwd + "/" + args[0].to_s) ; dir = Dir.getwd + "/" + args[0] | |
else ; raise "Invalid directory." | |
end | |
begin ; cont = Dir.entries(dir.to_s) ; cont.delete(".") ; cont.delete("..") | |
rescue ; raise "Failed to read directory." | |
end | |
if cont.length == 0 ; puts "Directory is empty" ; return nil ; end | |
str = [] ; fi = [] ; fo = [] | |
cont.each do |p| | |
if File.file?(dir.to_s + "/" + p.to_s) | |
begin ; s = File.size?(dir.to_s + "/" + p.to_s).to_s ; rescue ; s = "NO_READ_ACCESS" ; end | |
fi << "File: " + p.to_s + " Size: " + s.to_s | |
elsif File.directory?(dir.to_s + "/" + p.to_s) | |
fo << "Dir: " + p.to_s + "" | |
end | |
end | |
str << "Directory: \"" + dir.to_s + "\" Files: " + fi.length.to_s + ", Folders: " + fo.length.to_s | |
str << "#############################################" | |
fo.each { |f| str << f.to_s } ; fi.each { |f| str << f.to_s } | |
str << "#############################################\n" | |
puts str.join("\n").to_s | |
return nil | |
end | |
def map(dir) | |
if File.directory?(dir) == true ; elsif File.directory?(dir.to_s) == false and File.directory?(Dir.getwd + "/" + dir) == true ; dir = Dir.getwd + "/" + dir ; else ; raise "Invalid directory." ; end | |
remaining = [dir] ; current = nil ; failed = [] ; dirs = [] ; files = [] | |
loop do | |
if remaining.length == 0 ; break ; end | |
current = remaining[0] ; remaining.delete_at(0) | |
begin ; cont = Dir.entries(current) ; cont.delete(".") ; cont.delete("..") ; rescue ; failed << current ; next ; end | |
cont.each do |i| ; path = current + "/" + i ; if File.file?(path) ; files << path ; else ; dirs << path ; remaining << path ; end ; end | |
end | |
if failed.length == 0 ; return [dirs,files] ; else ; return [dirs,files,failed] ; end | |
end | |
def size(dir) | |
if File.directory?(dir) == true ; elsif File.directory?(dir.to_s) == false and File.directory?(Dir.getwd + "/" + dir) == true ; dir = Dir.getwd + "/" + dir ; else ; raise "Invalid directory." ; end | |
if File.directory?(dir.to_s) == false ; raise "Invalid directory." ; end | |
remaining = [dir] ; current = nil ; failed = [] ; dirs = [] ; size = 0 | |
loop do | |
if remaining.length == 0 ; break ; end | |
current = remaining[0] ; remaining.delete_at(0) | |
begin ; cont = Dir.entries(current) ; cont.delete(".") ; cont.delete("..") ; rescue ; failed << current ; next ; end | |
cont.each do |i| ; path = current + "/" + i ; if File.file?(path) ; begin ; size += File.size(path) ; rescue ; failed << path ; end ; else ; dirs << path ; remaining << path ; end ; end | |
end | |
if failed.length == 0 ; return size ; else ; return [size,failed] ; end | |
end | |
def empty?(dir) | |
if File.directory?(dir) == true ; elsif File.directory?(dir.to_s) == false and File.directory?(Dir.getwd + "/" + dir) == true ; dir = Dir.getwd + "/" + dir ; else ; raise "Invalid directory." ; end | |
begin ; cont = Dir.entries(dir) ; cont.delete(".") ; cont.delete("..") ; rescue ; raise "Failed to read directory." ; end | |
return cont.length == 0 | |
end | |
def delete!(dir) | |
if File.directory?(dir) == false ; raise "Invalid directory." ; end | |
remaining = [dir] ; current = nil ; failed = [] ; dirs = [] ; files = [] | |
loop do | |
if remaining.length == 0 ; break ; end | |
current = remaining[0] ; remaining.delete_at(0) | |
begin ; cont = Dir.entries(current) ; cont.delete(".") ; cont.delete("..") ; rescue ; failed << current ; next ; end | |
cont.each do |i| ; path = current + "/" + i ; if File.file?(path) ; files << path ; else ; dirs << path ; remaining << path ; end ; end | |
end | |
files.each do |fp| ; begin ; File.delete(fp) ; rescue ; failed << fp ; end ; end | |
dirs.reverse.each do |dp| ; begin ; Dir.delete(dp) ; if failed.include?(dp) ; failed.delete(dp) ; end ; rescue ; if failed.include?(dp) == false ; failed << dp ; end ; end ; end | |
begin ; Dir.delete(dir) ; rescue ; failed << dir ; end | |
if failed.length == 0 ; return true ; else ; raise "Failed to delete directory contents: " + failed.to_s ; end | |
end | |
def empty!(dir) | |
if File.directory?(dir) == false ; raise "Invalid directory." ; end | |
remaining = [dir] ; current = nil ; failed = [] ; dirs = [] ; files = [] | |
loop do | |
if remaining.length == 0 ; break ; end | |
current = remaining[0] ; remaining.delete_at(0) | |
begin ; cont = Dir.entries(current) ; cont.delete(".") ; cont.delete("..") ; rescue ; failed << current ; next ; end | |
cont.each do |i| ; path = current + "/" + i ; if File.file?(path) ; files << path ; else ; dirs << path ; remaining << path ; end ; end | |
end | |
files.each do |fp| ; begin ; File.delete(fp) ; rescue ; failed << fp ; end ; end | |
dirs.reverse.each do |dp| ; begin ; Dir.delete(dp) ; if failed.include?(dp) ; failed.delete(dp) ; end ; rescue ; if failed.include?(dp) == false ; failed << dp ; end ; end ; end | |
if failed.length == 0 ; return true ; else ; raise "Failed to delete directory contents: " + failed.to_s ; end | |
end | |
# def move(target, destination) | |
# end | |
# def copy(target, destination) | |
# end | |
# def rename(target, newname) | |
# end | |
# def search *args #3 target, string/[string], exclude type, exclude paths | |
# end | |
# def duplicate_file?(target) | |
# end | |
def image *args ## target, destination | |
if args.length == 0 ; raise "Invalid arguemnts. Requires a target directory." ; end | |
if File.directory?(args[0].to_s) == false ; raise "Invalid target directory." ; end | |
if args.length > 1 ; if args[1].to_s != "" and File.directory?(args[1]) == false ; raise "Invalid destination directory." ; end ; end | |
target = args[0].to_s ; destination = args[1].to_s | |
dirs = [] ; files = [] ; remaining = [target] ; cur = nil | |
begin | |
loop do | |
if remaining.length == 0 ; break ; end | |
cur = remaining[0] ; remaining.delete_at(0) | |
cont = Dir.entries(cur) ; cont.delete(".") ; cont.delete("..") | |
cont.each do |i| | |
p = cur + "/" + i | |
if File.file?(p) ; files << p ; elsif File.directory?(p) ; dirs << p ; remaining << p ; end | |
end | |
end | |
rescue ; raise "Failed to map target directory." | |
end | |
if dirs.length == 0 and files.length == 0 ; raise "Error, target directory is empty." ; end | |
package = "" ; write_position = 0 ; dir_mappings = [] ; file_mappings = [] | |
if dirs.length > 0 | |
dirs.each do |d| | |
p = d.to_s.split(target)[-1] ; package << p | |
dir_mappings << [write_position, write_position + (p.length - 1)] | |
write_position += p.length | |
end | |
end | |
begin | |
before_fail = nil | |
if files.length > 0 | |
files.each do |f| ; before_fail = f | |
fp = f.split(target)[-1] ; fi = File.open(f, "rb") ; fc = fi.read ; fi.close | |
fd = fp + fc ; package << fd | |
file_mappings << [write_position, write_position + (fd.length - 1), fp.length - 1] | |
write_position += fd.length | |
end | |
end | |
rescue ; raise "Failed to read file: " + before_fail | |
end | |
index = target.split("/")[-1] + ":" | |
ddm = [] ; dir_mappings.each do |dm| ; ddm << dm.join("?") ; end ; index << ddm.join(";") + "*" | |
ffm = [] ; file_mappings.each do |fm| ; ffm << fm.join("?") ; end ; index << ffm.join(";") | |
package = index + "|" + package | |
if destination == "" ; path = Dir.getwd + "/" + target.split("/")[-1] + ".img" ; else path = destination.to_s + "/" + target.split("/")[-1] + ".img" ; end | |
begin ; f = File.open(path, "wb") ; f.write(package) ; f.close ; rescue ; raise "Failed to write file." ; end | |
return true | |
end | |
def build_image *args ## target, destination | |
if args.length == 0 ; raise "Invalid arguments, requires a target file path." ; end | |
if File.file?(args[0].to_s) == false ; raise "No such file." ; end | |
if args.length > 1 ; if args[1].to_s != "" and File.directory?(args[1]) == false ; raise "Invalid destination directory." ; end ; end | |
target = args[0].to_s ; if args.to_s != "" ; destination = args[1].to_s ; else ; destination = Dir.getwd ; end | |
begin ; f = File.open(target, "rb") ; package = f.read ; f.close ; rescue ; raise "Failed to read target file." ; end | |
index = "" ; i = 0 | |
loop do ; if package[i] == "|" ; break ; end ; index << package[i] ; i += 1 ; end | |
package_data = package[Range.new((i+1),-1)] | |
data = index.split(":") | |
dirname = data[0] ; mappings = data[1].split("*") | |
dir_mappings = mappings[0].split(";") ; file_mappings = mappings[1].split(";") | |
dir_paths = [] ; file_paths = [] ; file_contents = [] | |
dir_mappings.each do |dm| | |
range = dm.split("?") ; range[0] = range[0].to_i ; range[1] = range[1].to_i | |
dir_paths << destination + "/" + dirname + package_data[Range.new(range[0].to_i,range[1].to_i)] | |
end | |
file_mappings.each do |fm| | |
range = fm.split("?") ; range[0] = range[0].to_i ; range[1] = range[1].to_i ; range[2] = range[2].to_i | |
file_paths << destination + "/" + dirname + package_data[Range.new(range[0].to_i,(range[0].to_i + range[2].to_i))] | |
file_contents << package_data[Range.new((range[0]+(range[2]+1)),range[1])] | |
end | |
if File.directory?(destination + "/" + dirname) == true ; raise "ERROR target directory already exists." ; end | |
begin ; Dir.mkdir(destination + "/" + dirname) ; rescue ; raise "Failed to create target directory." ; end | |
before_fail = nil ; dir_paths.each do |dp| ; begin ; before_fail = dp ; Dir.mkdir(dp) ; rescue ; raise "Failed to create directory: " + before_fail ; end ; end | |
i = 0 ; before_fail = nil | |
begin ; file_paths.each do |fp| ; f = File.open(fp, "wb") ; f.write(file_contents[i]) ; f.close ; i += 1 ; end | |
rescue ; raise "Failed to write file: " + before_fail.to_s | |
end | |
return true | |
end | |
# def zip *args | |
# end | |
# def unzip *args | |
# end | |
alias :make :mkdir | |
} | |
def dir *args ; Dir.dir *args ; end | |
def viewdir *args ; puts Dir.view *args ; end | |
alias :vd :viewdir | |
File.instance_eval{ | |
alias :dir? :directory? | |
} | |
Integer.class_eval{ | |
def exponate | |
number = self; base = 2; exponent = 2 | |
while base <= number | |
exponent = 2 | |
while exponent < number ; if base ** exponent == number ; return [base, exponent] ; end ; exponent += 1 ; end | |
base += 1 | |
end | |
end | |
def factors | |
n = self; factor1 = 1; factor2 = n | |
(2..Math.sqrt(n)).each do |factor| ; if n % factor == 0 ; factor1 = factor ; factor2 = n / factor ; break ; end ; end | |
return [factor1, factor2] | |
end | |
def prime? | |
return false if self <= 1 | |
(2..Math.sqrt(self)).each do |i| ; return false if self % i == 0 ; end | |
return true | |
end | |
def surname *args | |
if args.length == 0; int=self.to_s ; else; int=args[0].to_s; end | |
if int.to_s=="0";int="0" | |
elsif int[-2..-1]=="11" or int[-2..-1] =="12" or int[-2..-1] =="13";int<<"th" | |
elsif int[-1]=="1";int<<"st" | |
elsif int[-1]=="2";int<<"nd" | |
elsif int[-1]=="3";int<<"rd" | |
else;int<<"th" | |
end | |
return int | |
end | |
def commas; self.to_s.reverse.gsub(/(\d{3})(?=\d)/, '\\1,').reverse; end | |
} | |
#8#;#3#;#5#;#3#;#1#;#8#;#3#;#5#;#3#;#8#;#5##string.rb#5#;#6#;#9#;#9#;#4#;#5#;#6#;#7#;#9#;#6#;#4##string.rb | |
String.class_eval{ | |
def shuffle ; return self.split('').shuffle.join('').to_s ; end | |
alias :scramble :shuffle | |
def base10? ; self.delete("0123456789").empty? ; end | |
alias :only_numbers? :base10? | |
def base16? ; self.upcase.delete("0123456789ABCDEF").empty? ; end | |
alias :only_hex? :base16? | |
def base36? ; self.upcase.delete("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ").empty? ; end | |
def only_letters? ; self.upcase.delete("ABCDEFGHIJKLMNOPQRSTUVWXYZ").empty? ; end | |
def to_binary ##returns a list of the binary byte form of the string from utf8 only(cause ruby) | |
b = [] ; s = self.to_s.split('') | |
s.each do |ch| | |
b << BINARY[CHARS.index(ch.to_s)] | |
end | |
return b.join.to_s | |
end | |
def from_binary ## works on strings used with .to_b restoring them to ascii characters | |
bytes = [] ; s = self.to_s | |
until s.to_s.length == 0 | |
b = s[0..7].to_s ; s = s[8..-1] | |
bytes << b.to_s | |
end | |
str = '' | |
bytes.each do |b| | |
str << CHARS[BINARY.index(b.to_s).to_i].to_s | |
end | |
return str.to_s | |
end | |
alias :ew? :end_with? | |
alias :sw? :start_with? | |
##in building it this way, i can take this and make an actual code parser | |
def splice(b,e) | |
if b.is_a?(String) == false or e.is_a?(String) == false; raise "Arguements require String type." | |
elsif b.to_s=="" or e.to_s == ""; raise "Arguements cannot be nilstring." | |
end | |
s=self ## self inside the loop will not be the string | |
if s.length<=(b.to_s.length+e.to_s.length); raise "Base string is too small."; end | |
pos=0; stack = 0; list=[] | |
if b.length > e.length ; buffer_length = b.length; else; buffer_length = e.length; end | |
buffer = []; buffer_length.times{ buffer << "" } | |
empty_buffer=[]; buffer_length.times{ empty_buffer << "" } ## again, in the loop we can only refer to vars | |
empty_buffer2=[]; buffer_length.times{ empty_buffer2 << "" } ## im really not sure why but id love to know | |
tag1=empty_buffer; b.split('').each { |ch| empty_buffer << ch ; empty_buffer.delete_at(0) } | |
tag2=empty_buffer2; e.split('').each { |ch| empty_buffer2 << ch ; empty_buffer2.delete_at(0) } | |
loop do ## in this loop s will be self and tag1 & 2 will point to the buffers we want to work with | |
if s[pos].to_s==""; break; end | |
buffer << s[pos]; buffer.delete_at(0) | |
if stack > 0 ; list << s[pos]; end | |
str=buffer.join(''); tag = tag1.join('') | |
m=true; i=0 | |
tag.reverse.split('').each{ |ch| if ch.to_s!=str.reverse[i].to_s and ch.to_s != ""; m=false; break; end; i+=1 } | |
if m == true; stack += 1; end | |
tag = tag2.join('') | |
m=true; i=0 | |
tag.reverse.split('').each{ |ch| if ch.to_s!=str.reverse[i].to_s and ch.to_s != ""; m=false; break; end; i+=1 } | |
if m == true; stack -= 1; end | |
pos+=1 | |
end | |
if list.length == 0; return [] ##not nilstring so we can tell the difference on return side | |
else; return list.join('')[0..("-"+(e.length+1).to_s).to_i].split(e) | |
end | |
end | |
def numerize | |
str = "" ; i = 0 | |
loop do | |
if self[i] == nil ; break ; end | |
ordinal = self[i].ord.to_s ; loop do ; if ordinal.length >= 3 ; break ; end ; ordinal = "0" + ordinal ; end | |
str << ordinal ; i += 1 | |
end | |
return str | |
end | |
def denumerize | |
str = "" ; i = 0 | |
loop do | |
if self[i] == nil ; break ; end | |
str << self[i..(i+2)].to_i.chr ; i += 3 | |
end | |
return str | |
end | |
alias :spl :split | |
def parse_array ; return String.parse_array(self) ; end | |
def parse_hash ; return String.parse_hash(self) ; end | |
} | |
String.instance_eval{ | |
def parse_array(str) | |
if str.to_s == "[]"; return []; end | |
str = str.strip.gsub(/^\[|\]$/, '') | |
elements = []; current_element = ''; nested_level = 0 | |
str.each_char do |c| | |
if c == ',' && nested_level == 0 | |
elements << current_element.strip | |
current_element = '' | |
else | |
current_element += c | |
if c == '['; nested_level += 1 | |
elsif c == ']'; nested_level -= 1 | |
end | |
end | |
end | |
elements << current_element.strip | |
elements.map do |element| | |
if element.start_with?('"') && element.end_with?('"') ; element.gsub(/^"|"$/, '') | |
elsif element.start_with?("'") && element.end_with?("'") ; element.gsub(/^'|'$/, '') | |
elsif element =~ /\A\d+\z/ ; element.to_i | |
elsif element =~ /\A\d+\.\d+\z/ ; element.to_f | |
elsif element == 'true' || element == 'false' ; element == 'true' | |
elsif element.start_with?('[') && element.end_with?(']') ; String.parse_array(element) | |
elsif element.start_with?('{') && element.end_with?('}') ; String.parse_hash(element) | |
else ; element | |
end | |
end | |
end | |
def parse_hash(str) | |
if str.to_s == "{}"; return {}; end | |
str = str.strip.gsub(/^\{|\}$/, '') | |
pairs = []; current_key = ''; current_value = ''; nested_level = 0 | |
str.each_char do |c| | |
if c == '>' && nested_level == 0 | |
current_key = current_value.gsub(/['":]\s*(\w+)\s*['":]?/, '\1'); current_key = current_key.delete(" =") | |
current_value = '' | |
elsif c == ',' && nested_level == 0 | |
pairs << [current_key, current_value.strip] | |
current_key = '' | |
current_value = '' | |
else | |
current_value += c | |
if c == '{' || c == '[' ; nested_level += 1 | |
elsif c == '}' || c == ']' ; nested_level -= 1 | |
end | |
end | |
end | |
pairs << [current_key, current_value.strip] | |
hash = {} | |
pairs.each do |pair| | |
key = pair[0] | |
value = pair[1] | |
if value.start_with?('{') && value.end_with?('}') ; hash[key] = String.parse_hash(value) | |
elsif value.start_with?('[') && value.end_with?(']') ; hash[key] = String.parse_array(value) | |
elsif value == 'true' ; hash[key] = true | |
elsif value == 'false' ; hash[key] = false | |
elsif value =~ /\A\d+\z/ ; hash[key] = value.to_i | |
elsif value =~ /\A\d+\.\d+\z/ ; hash[key] = value.to_f | |
else ; hash[key] = value.gsub(/^\"|\"$/, '') | |
end | |
end | |
return hash | |
end | |
} | |
Time.class.class.class_eval{ | |
def parse_seconds(s) | |
s = s.to_i | |
if s < 60 ; return [0, 0, s] | |
elsif s < 3600 ; return [0, s / 60, s % 60] | |
elsif s < 86400 ; return [s / 3600, (s / 60) % 60, s % 60] | |
else ; days = s / 86400 ; hours = (s / 3600) % 24 ; minutes = (s / 60) % 60 ; seconds = s % 60 ; return [days, hours, minutes, seconds] | |
end | |
end | |
def stamp *args | |
if args.length == 0 ; return Time.now.year.to_s + "." + Time.now.mon.to_s + "." + Time.now.day.to_s + "." + Time.now.hour.to_s + "." + Time.now.min.to_s + "." + Time.now.sec.to_s ; end | |
if args[0].is_a?(Time) ; return args[0].year.to_s + "." + args[0].mon.to_s + "." + args[0].day.to_s + "." + args[0].hour.to_s + "." + args[0].min.to_s + "." + args[0].sec.to_s | |
elsif args[0].is_a?(String) and args[0].to_s.length > 0 | |
t = args[0].split(".") | |
begin ; stamp = Time.new(t[0].to_i, t[1].to_i, t[2].to_i, t[3].to_i, t[4].to_i, t[5].to_i) | |
rescue ; raise "Invalid time stamp provided, must be formatted like this: 0000.1.1.0.0.0 - 9999.12.31.23.59.59" | |
end | |
return stamp | |
else ; raise "Invalid arguments, requires a Time or String object." | |
end | |
end | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment