PageRenderTime 7ms CodeModel.GetById 2ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/trunk/Examples/ruby/free_function/runme.rb

#
Ruby | 46 lines | 31 code | 7 blank | 8 comment | 1 complexity | 21ca49dead6bd4fa2e93a7f48d3dae60 MD5 | raw file
 1require 'example'
 2
 3begin
 4  begin
 5    # Create an animal and zoo
 6    tiger1 = Example::Animal.new("tiger1")
 7    zoo = Example::Zoo.new
 8  
 9    # At the animal to the zoo - this will transfer ownership
10    # of the underlying C++ object to the C++ zoo object
11    zoo.add_animal(tiger1)
12
13    # get the id of the tiger
14    id1 = tiger1.object_id
15
16    # Unset the tiger
17    tiger1 = nil
18  end
19
20  # Force a gc
21  GC.start
22
23  # Get the tiger and its id
24  tiger2 = zoo.get_animal(0)
25  id2 = tiger2.object_id
26
27  # The ids should not be the same
28  if id1==id2
29    raise RuntimeError, "Id's should not be the same"
30  end
31
32  zoo = nil
33end
34
35GC.start
36
37# This method is no longer valid since the zoo freed the underlying
38# C++ object
39ok = false
40begin
41  puts tiger2.get_name
42rescue ObjectPreviouslyDeleted => error
43  ok = true
44end
45
46raise(RuntimeError, "Incorrect exception raised - should be ObjectPreviouslyDeleted") unless ok