File: core_ext.rb

package info (click to toggle)
ruby-bogus 0.1.6-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, stretch
  • size: 856 kB
  • ctags: 640
  • sloc: ruby: 4,219; makefile: 7; sh: 2
file content (26 lines) | stat: -rw-r--r-- 778 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# This monkey patch should not break Ruby compatibility
# but is necessary because MRI, instead of calling object.kind_of?(module),
# calls the C function, which implements kind_of. This makes
# using fake objects in switch cases produce unexpected results:
#
#   fake = fake(:library) { Library }
#
#   case fake
#   when Library then "bingo!"
#   else raise "oh noes!"
#   end
#
# Without the patch, the snippet above raises 'oh noes!'
# instead of returning 'bingo!'.
class Module
  # don't warn about redefining === method
  Bogus::Support.supress_warnings do
    alias :__trequals__ :===

    def ===(object)
      # BasicObjects do not have kind_of? method
      return __trequals__(object) unless Object.__trequals__(object)
      object.kind_of?(self)
    end
  end
end