大量的小对象在 ruby 中存储也会占用很大的内存。 比如游戏开发中配置文件 excel,可能十几 M, 加载到 ruby 中占用 好几百 M 内存. 使用 SMatrix 后,又只占用几 M 了。
https://github.com/libinzhangyuan/s_matrix
EQUIPMENTS = SMatrix.new EQUIPMENTS.add_row('10001', {'id' => '10001', 'name' => 'wood sword', 'attack' => '342'}) EQUIPMENTS.add_row('20002', {id: 10002, name: 'shoe', speed: 5})
EQUIPMENTS['10001'].should == {'id' => '10001', 'name' => 'wood sword', 'attack' => '342''} EQUIPMENTS['10001']['id'].should == '10001' EQUIPMENTS['10001'][:id].should == '10001' EQUIPMENTS['20002'].should == {'id' => '10002', 'name' => 'shoe', 'speed' => '5'} EQUIPMENTS['20002']['attack'].should == '552' EQUIPMENTS.size.should == 2 # or you can use get_row instead of [] EQUIPMENTS.get_row('20002')
EQUIPMENTS.each {|k, v| k == '10001', v == {'id' => '10001', 'name' => 'sword', ...} }
EQUIPMENTS.all.should == { '20002' => {'id' => '10002', 'name' => 'shoe', 'speed' => '5'}, '20003' => {} }
EQUIPMENTS.first.should == ['20002', {'id' => '10002', 'name' => 'shoe', 'speed' => '5'}] # ids EQUIPMENTS.ids.should == ['20002', '20003']
EQUIPMENTS.keys.should == ['id', 'name', 'hp']
EQUIPMENTS.find({name: 'shoe'}).should == ['20002', {'id' => '10002', 'name' => 'shoe', 'speed' => '5'}] # to_s for debug
使用 c++ 开发 高质量代码,有测试,请放心使用。 谢谢关注。