Advertisement
DRVTiny

How_Passed_Instance_Vars.cr

May 8th, 2018
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.82 KB | None | 0 0
  1. class Some
  2.   property s : String, ai : Array(Int32)
  3.  
  4.   def initialize(@s : String, *some_ints)
  5.     @ai = [] of Int32
  6.     some_ints.each { |e|
  7.       if e.is_a?(Int32)
  8.         @ai << e
  9.       end
  10.     }
  11.   end
  12.  
  13.   def s_addr
  14.     @s.to_unsafe.address
  15.   end
  16.  
  17.   def ai_dump
  18.     p @ai
  19.   end
  20. end
  21.  
  22. obj = Some.new("kis-kis", 1, 2, 3)
  23. copy_of_s = obj.s
  24. ps = pointerof(copy_of_s)
  25.  
  26. puts %(Address in memory of "copied" s: #{copy_of_s.to_unsafe.address})
  27. puts %(Address in memory of instance variable stored inside object: #{obj.s_addr})
  28. puts %("copied" s and instance variable is ) + (copy_of_s.to_unsafe.address == obj.s_addr ? "absolutely" : "not") + " the same"
  29.  
  30. l = obj.ai
  31. puts %(Array inside object before change to its "copy")
  32. obj.ai_dump
  33. l[0] = 7
  34. puts %(Array inside object after change to its "copy")
  35. obj.ai_dump
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement