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

# · Ruby · 46 lines · 31 code · 7 blank · 8 comment · 1 complexity · 21ca49dead6bd4fa2e93a7f48d3dae60 MD5 · raw file

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