Skip to content

Instantly share code, notes, and snippets.

@thomasjslone
Created September 17, 2016 00:10
Show Gist options
  • Save thomasjslone/90a589561e791f4e235a7061a881b559 to your computer and use it in GitHub Desktop.
Save thomasjslone/90a589561e791f4e235a7061a881b559 to your computer and use it in GitHub Desktop.
datacase template
class List_Keeper
def initialize
@list = []
@kbchars = ["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","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","~","`","!","@","#","$","%","^","&","*","(",")","-","_","=","+","[","]","{","}",";",":","\"","'","\\","/","|","?","<",">",",","."," ","\t","\n"]
@chbytes = []
@kbchars.each { |ch| @chbytes << ch.bytes[0].to_s }
self.load_list
self.shell
end
def load_list
fi = File.open(__FILE__,"r")
ec = fi.read.to_s.split("#D#;#A#;" + "#T#;#A#")[-1].to_s[1..-1].split(":")
fi.close
list = []
ec.each { |eobj| list << self.denumerize(eobj.to_s) }
@list = list
true
end
def save_list
ec = []
@list.each { |obj| ec << self.numerize(obj.to_s).to_s }
img = "#" + ec.join(":").to_s
fi = File.open(__FILE__,"r")
fc = fi.read.to_s
fi.close
file = fc.split("#D#;#A#;" + "#T#;#A#")[0].to_s
fi = File.open(__FILE__,"w")
img = file.to_s + "#D#;#A#;" + "#T#;#A#\n" + img.to_s
fi.write(img.to_s)
fi.close
true
end
def add(obj)
if @list.include?(obj) == false
@list << obj
true
else
false
end
end
def del
if @list.include?(obj)
@list.delete(obj)
true
else
false
end
end
def rmi(i)
if i.to_i + 1 <= @list.length.to_i
@list.delete_at(i.to_i)
true
else
false
end
end
def search(term)
f = []
@list.each do |obj|
if obj.to_s.downcase.include?(term.to_s.downcase)
f << obj.to_s
end
end
if obj.length == 0
false
else
return obj
end
end
def numerize(str) # this will turn your strings into a list of positive numbers that never starts with 0, this is useful if you wish to perform an encryption algorithm on the string
ch_inds = []
str.to_s.split("").each { |ch| ch_inds << @chbytes.index(ch.to_s.ord.to_s) }
enc = []
ch_inds.each do |ch|
code = ch.to_i + 1
if code.to_s.length == 1
code = "0" + code.to_s
else
code = code.to_s
end
enc << code.to_s
end
enc = enc.join("").to_s
if enc.to_s[0].to_s == "0"
enc = enc[1..-1].to_s
end
return enc.to_s
end
def denumerize(str)
if str.to_s.length.odd?
str = "0" + str.to_s
end
str_codes = []
i = 0
hold = ""
str.to_s.split("").each do |num|
if i.to_i == 0
hold << num.to_s
i += 1
elsif i.to_i == 1
hold << num.to_s
i = 0
str_codes << hold.to_s
hold = ""
end
end
str = str_codes
str_codes = []
str.each do |c|
str_codes << c.to_i - 1
end
dec_str = ""
str_codes.each { |c| dec_str << @kbchars[c.to_i].to_s }
return dec_str.to_s
end
def shell
@shell = true
while @shell
res = "Nil Response"
print "<< "
inp = gets.chomp.to_s
if inp.to_s == "size"
res = "List Population: " + @list.length.to_s
elsif inp.to_s[0..3] == "add:"
op = self.add(inp[4..-1].to_s)
if op
res = "Object added."
else
res = "Object already exists in the list at index: " + @list.index(inp.to_s[4..-1].to_s).to_s
end
elsif inp.to_s[0..3] == "del:"
d = 0
obj = inp[4..-1].to_s.split(" ")
obj.each do |ob|
if self.del(ob)
d += 1
end
end
res = d.to_s + " objects were deleted from the list."
elsif inp.to_s[0..3] == "rmi:"
elsif inp.to_s[0..6] == "search:"
elsif inp.to_s[0] == "<"
begin
res = eval(inp[1..-1].to_s).to_s
rescue => exception
res = exception.to_s
end
elsif inp.to_s == "save"
self.save_list
res = "List saved."
elsif inp.to_s == "load"
self.load_list
res = "List loaded."
elsif inp.to_s[0..3] == "view"
if inp.to_s == "view"
res = "List: \n - " + @list.join("\n - ").to_s
end
else
res = "Invalid Input"
end
print ">> " + res.to_s + "\n"
end
@shell = false
end
end
$list = List_Keeper.new
#D#;#A#;#T#;#A#
#67420119192315180419:10011105:1325011919
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment