cloneable.rb 665 B

12345678910111213141516171819202122232425
  1. module Rake
  2. # ##########################################################################
  3. # Mixin for creating easily cloned objects.
  4. #
  5. module Cloneable
  6. # Clone an object by making a new object and setting all the instance
  7. # variables to the same values.
  8. def dup
  9. sibling = self.class.new
  10. instance_variables.each do |ivar|
  11. value = self.instance_variable_get(ivar)
  12. new_value = value.clone rescue value
  13. sibling.instance_variable_set(ivar, new_value)
  14. end
  15. sibling.taint if tainted?
  16. sibling
  17. end
  18. def clone
  19. sibling = dup
  20. sibling.freeze if frozen?
  21. sibling
  22. end
  23. end
  24. end