Skip to content

Instantly share code, notes, and snippets.

@glurp
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save glurp/a38510e0209b5a7af220 to your computer and use it in GitHub Desktop.

Select an option

Save glurp/a38510e0209b5a7af220 to your computer and use it in GitHub Desktop.
ruby Pico dabase , ACID
def sql
fn="rdb.db"
if defined?($data)
( yield( a=Marshal.load(Marshal.dump($data)) ) ;
$data=a ; File.open(fn,"rb+") { |f| Marshal.dump($data,f) }
) rescue p $!
return
end
File.exists?(fn) || File.write(fn,"")
File.open(fn,"rb+") do |f|
data= File.size(fn)>0 ? Marshal.load(f) : {}
f.rewind
begin
yield(data)
$data=data
Marshal.dump($data,f)
rescue Exception => e
p $!
end
end
end
sql { |db| db[:table1]=(0..1000).map {|i| {id: i, body:""} } unless db[:table1] }
22.upto(25) { |i| sql { |db| p db[:table1][i][:body]="Hello #{i}" } }
sql { |db| p db[:table1][23] }
sql { |db| db[:table1][23]={junk:[]} ; raise("error") }
sql { |db| p db[:table1][23] }
sql { |db| p db[:table1].select {|rec| rec[:id]%343==0 } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment