From: Rob Pitt Date: 2006-06-17T21:11:11+09:00 Subject: Why does this code traversing iv_tbl using dl/struct via evil.rb not work? --Apple-Mail-1-440369917 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset=US-ASCII; delsp=yes; format=flowed Hi all, I seem to have misunderstood some finer point of how the instance variable tables work. Since for something like this I expect code speaks louder than words, I have made a small test case using evil.rb (to keep the file size down) available from: http://rubyforge.org/cgi-bin/viewvc.cgi/evil/lib/evil.rb? revision=1.44&root=evil Unfortunately, due to some error I have made this test case does not find all the instance variable keys. I'm sure some wizard out there will easily spot my newbie mistake. Could someone please point out where I'm going wrong by making this tiny test case work? Many thanks, Rob. --Apple-Mail-1-440369917 Content-Transfer-Encoding: 7bit Content-Type: text/x-ruby-script; x-unix-mode=0644; name="test_evil_structs.rb" Content-Disposition: attachment; filename=test_evil_structs.rb require 'evil' require 'test/unit' class A;end class TestSHStructs < Test::Unit::TestCase include RubyInternal def test_iv_tbl_traverse a = A.new located_keys = [] created_keys = ['@foo', '@bar', '@baz', '@dave', '@jess', '@sally'] created_keys.each do |key| a.instance_variable_set(key,1) end r_object = RObject.new(a.internal_ptr) table = ST_TABLE.new(r_object.iv_tbl) chain = 0 while chain < table.num_bins table_entry = ST_TABLE_ENTRY.new(table.bins + (chain * DL::sizeof('P'))) while table_entry sym = table_entry.key.to_sym located_keys << sym.to_s if sym && sym.to_s[0] == ?@ table_entry = table_entry.next && ST_TABLE_ENTRY.new(table_entry.next) end chain += 1 end assert_equal(created_keys.sort,located_keys.sort) end end --Apple-Mail-1-440369917--