Skip to content

Instantly share code, notes, and snippets.

@bknarendra
Created December 26, 2012 10:15
Show Gist options
  • Save bknarendra/4379420 to your computer and use it in GitHub Desktop.
Save bknarendra/4379420 to your computer and use it in GitHub Desktop.
A simple No-SQL key-value db using self-modifying ruby script: An interesting application of Ruby's Reflection API
hash={"1"=>"Narendra", "2"=>"Mangesh", "3"=>"Viru", "4"=>"Virendra", "5"=>"Genh"}
z=[]
err_msg="ruby #{__FILE__} <GET/SET> <key_to_search/key_to_set> <not_required/value_to_set>"
if ARGV.length<2 && (ARGV[0]!="GET" || ARGV[0]!="SET")
puts err_msg
exit
end
if ARGV[0]=="SET" && ARGV.length!=3
puts err_msg
exit
end
File.open(__FILE__,'r'){|f| f.each_line {|l| z<<l}}
c=eval(z[0])
if ARGV[0]=="GET"
puts c[ARGV[1]] if c.include?(ARGV[1])
elsif ARGV[0]=="SET"
c[ARGV[1]]=ARGV[2]
z[0]="hash="+c.inspect+"\n"
File.open(__FILE__,'w'){|f| z.each{|x| f<<x}}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment