/tools/Ruby/lib/ruby/1.8/test/unit/util/procwrapper.rb

http://github.com/agross/netopenspace · Ruby · 48 lines · 27 code · 7 blank · 14 comment · 1 complexity · 5988291605cf32cbb930cd999f71c26b MD5 · raw file

  1. #--
  2. #
  3. # Author:: Nathaniel Talbott.
  4. # Copyright:: Copyright (c) 2000-2002 Nathaniel Talbott. All rights reserved.
  5. # License:: Ruby license.
  6. module Test
  7. module Unit
  8. module Util
  9. # Allows the storage of a Proc passed through '&' in a
  10. # hash.
  11. #
  12. # Note: this may be inefficient, since the hash being
  13. # used is not necessarily very good. In Observable,
  14. # efficiency is not too important, since the hash is
  15. # only accessed when adding and removing listeners,
  16. # not when notifying.
  17. class ProcWrapper
  18. # Creates a new wrapper for a_proc.
  19. def initialize(a_proc)
  20. @a_proc = a_proc
  21. @hash = a_proc.inspect.sub(/^(#<#{a_proc.class}:)/){''}.sub(/(>)$/){''}.hex
  22. end
  23. def hash
  24. return @hash
  25. end
  26. def ==(other)
  27. case(other)
  28. when ProcWrapper
  29. return @a_proc == other.to_proc
  30. else
  31. return super
  32. end
  33. end
  34. alias :eql? :==
  35. def to_proc
  36. return @a_proc
  37. end
  38. end
  39. end
  40. end
  41. end