Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Last active September 13, 2021 11:15
Show Gist options
  • Save thomasjslone/837634c0eb9e45760b09df4f82ea1072 to your computer and use it in GitHub Desktop.
Save thomasjslone/837634c0eb9e45760b09df4f82ea1072 to your computer and use it in GitHub Desktop.
## all 256 ascii characters
CHARS = [] ; c = 0 ; 256.times{ CHARS << c.chr.to_s ; c += 1 }
## every 8 bit binary number in order
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 }
## every hexicdeimal number in order upto 256 ordinals
HEX = [] ; c = 0 ; 256.times { h = c.to_s(16) ; if h.length == 1 ; h = "0" + h.to_s ; end ; HEX << h ; c += 1 }
## a list of all 8 bit byte codes for the ascii characters
BYTES = [] ; HEX.each do |h| ; BYTES << "\\x" + h ; end
def only_letters? s ;s.upcase.delete("ABCDEFGHIJKLMNOPQRSTUVWXYZ").empty?;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 == 1 or a == 1 and b == 0 ; return 1 ; else ; return 0 ; 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 or a == 0 and b == 1 or a == 1 and b == 0 ; return 1 ; else ; return 0 ; end ; end
def _nand(a,b) ; if a == 1 and b == 1 or a == 0 and b == 0 ; return 1 ; else ; return 0 ; end ; end
def _xor(a,b) ; if a == 0 and b == 1 or a == 1 and b == 0 or a == 1 and b == 1 ; return 1 ; else return 0 ; end ; end
def rands(*int);if int[0].to_i>=2;t=int[0].to_i;else;t=1;end;s='';t.times{s='';s<<rand(255).chr};return s;end
def parse_seconds(s) ; s = s.to_s.to_f ; if s.to_f < 60.0 ; [0,0,s.to_i] ; elsif s.to_f < 3600.0 and s.to_f >= 60.0 ; minutes = s.to_f / 60.to_f ; sec = ("." + minutes.to_s.split(".")[-1].to_s).to_f * 60 ; [0,minutes.to_i, sec.round] ; elsif s.to_f < 86400.0 and s.to_f >= 3600.0 ; hours = s.to_f / 60.to_f / 60.to_f ; minutes = ("." + hours.to_s.split(".")[-1].to_s).to_f * 60 ; sec = ("." + minutes.to_s.split(".")[-1].to_s).to_f * 60 ; [hours.to_i, minutes.to_i ,sec.to_i] ; else ; s.to_s + "MORE THAN 24 HOURS" ; end ; end
def timestamp *args
if args[0] == nil ; t = Time.now ; y = t.year.to_s ; mo = t.month.to_s ; d = t.day.to_s ; h = t.hour.to_s ; mi = t.min.to_s ; s = t.sec.to_s ; if y.length < 4 ; until y.length == 4 ; y = "0" + y ; end ; end ; y = y[0..3] ; if mo.length < 2 ; mo = "0" + mo end ; mo = mo[0..1] ; if d.length < 2 ; d = "0" + d ; end ; d = d[0..1] ; if h.length < 2 ; h = "0" + h ; end ; h = h[0..1] ; if mi.length < 2 ; mi = "0" + mi ; end ; mi = mi[0..1] ; if s.length < 2 ; s = "0" + s ; end ; s = s[0..1] ; [y,mo,d,h,mi,s].join(".")
elsif args[0].is_a? Time ; t = args[0] ; y = t.year.to_s ; mo = t.month.to_s ; da = t.day.to_s ; hr = t.hour.to_s ; mi = t.min.to_s ; se = t.sec.to_s ; if mo.length == 1 ; mo = "0" + mo.to_s ; end ; if da.length == 1 ; da = "0" + da.to_s ; end ; if hr.length == 1 ; hr = "0" + hr.to_s ; end ; if mi.length == 1 ; mi = "0" + mi.to_s ; end ; if se.length == 1 ; se = "0" + se.to_s ; end ; return y.to_s + "." + mo.to_s + "." + da.to_s + "." + hr.to_s + "." + mi.to_s + "." + se.to_s
elsif args[0].is_a? String ; t = args[0].split(".") ; return Time.new(t[0],t[1],t[2],t[3],t[4],t[5])
end
end
def commas strinp ##can be an integer or string
str = ""
s = strinp.to_s.split("").reverse ; i=0
s.each do |nc|
if i == 2
i=0 ; str << nc.to_s + ","
else
str << nc.to_s ; i+=1
end
end
if str.to_s[-1].chr.to_s == ","
str = str.reverse.to_s.split("")[1..-1].join("").to_s
else
str = str.reverse.to_s
end
return str.to_s
end
def factors n ##this code is ugly redesign it or at least rewrite it, i barley remember how it works it manually checks two numbers counting them up until they are factors of the given number
p = [2] ; vn = 2
until vn == n
vn += 1
p << vn
end
p.delete_at(-1)
f1 = 0 ; f2 = 0 ; pd = []
p.each { |pn|
s = n.to_f / pn.to_f
if s.to_s[-2..-1].to_s == ".0"
pd << pn
end
}
pd.each { |p|
if p * p == n4
f1, f2 = p, p
else
cd = pd
cd.delete(p)
cd.each { |pr|
if p * pr == n
f1, f2 = p, pr
break
end
}
end
}
return [f1,f2]
end
def prime? n ## manually checks an integer to see if its a prime number
rv=true;c=1;l=n-1 ##the counter will run from 2 through one below the integer and divide all of them, if none are whole numbers then the given integer was a prime number
until c>=l ##this became a greater than or equal due to a bug but can probably be turned back to ==, dont feel like testing it tho so imma leave it right now
c+=1;if (n.to_f/c.to_f).to_s.split(".")[-1].to_i==0;rv=false;c=n;end
end
return rv
end
def eval_prime n ## same as prime? but this one saves its work to the dir ruby is working from
rv=true ; work=[]
c=1;l=n-1
until c>=l
c+=1
str= "c: "+c.to_s
r=(n.to_f/c.to_f).to_s
str<<" r: "+r.to_s
if r.split(".")[-1].to_i==0
rv=false;c=n
end
work<<str
puts str.to_s
end
f=File.open("EvalPrime results for "+n.to_s+".txt","w");f.write(work.join("\n"));f.close
return rv
end
def mapdir(dir) ## returns an array listing all files and subdirectories that are readable in the given directory
if File.directory?(dir.to_s) or File.directory?(Dir.getwd.to_s + "/" + dir.to_s)
if File.directory?(dir.to_s) ; ; else ; dir = Dir.getwd.to_s + "/" + dir.to_s ; end
cur = nil ; rem = [dir.to_s] ; fi = [] ; fo = [] ; ex = []
until rem.length == 0
cur = rem[0].to_s ; rem.delete_at(0)
begin ; cont = Dir.entries(cur.to_s) ; cont.delete(".") ; cont.delete("..")
if cont.length > 0
cont.each do |p|
if File.file?(cur.to_s + "/" + p.to_s) ; fi << cur.to_s + "/" + p.to_s
elsif File.directory?(cur.to_s + "/" + p.to_s) ; fo << cur.to_s + "/" + p.to_s ; rem << fo[-1]
end
end
end
rescue
ex << cur
end
end
if ex.length == 0 ; return [fi,fo]
else ; return [fi,fo,ex]
end
elsif File.file?(dir.to_s) ; return "Arguement is a file. Dir.map returns arrays of subdirectories and files with in a directory, it does not work on files."
else
raise "No such directory"
end
end
Dir.instance_eval{
def dir *args
if args[0] == nil ; 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
elsif File.file?(args[0].to_s) ; Dir.chdir(args[0].to_s.split("/")[0..-2].join("/").to_s) ; return Dir.getwd.to_s
else ; raise "No such directory."
end
else ; raise "Invalid arguements, use a string to represent a path."
end
end
)
## lets add dandy shortcuts for long methods we hate typing
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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment