This page contains automated test results for code from O'Reilly's Ruby Cookbook. If this code looks interesting or useful, you might want to buy the whole book.
Checking Class or Module Membership | ||
---|---|---|
Code | Expected | Actual |
def send_as_package(obj) if obj.respond_to? :package packaged = obj.package else if $DEBUG $stderr.puts "Not sure how to package a #{obj.class}." $stderr.puts 'Trying generic packager.' end package = Package.new(obj) end send(package) end def multiply_precisely(a, b) if a.is_a? Float or b.is_a? Float raise ArgumentError, "I can't do precise multiplication with floats." end a * b end multiply_precisely(4, 5) |
20 | 20 |
multiply_precisely(4.0, 5) |
ArgumentError: I can't do precise multiplication with floats. ... |
ArgumentError: I can't do precise multiplication with floats. from (irb):15:in `multiply_precisely' from (irb):20 |
def append_to_self(x) unless x.respond_to? :<< raise ArgumentError, "This object doesn't support the left-shift operator." end if x.is_a? Numeric raise ArgumentError, "The left-shift operator for this object doesn't do an append." end x << x end append_to_self('abc') |
"abcabc" | "abcabc" |
append_to_self([1, 2, 3]) |
[1, 2, 3, [...]] | [1, 2, 3, [...]] |
append_to_self({1 => 2}) |
ArgumentError: This object doesn't support the left-shift operator. ... |
ArgumentError: This object doesn't support the left-shift operator. from (irb):23:in `append_to_self' from (irb):33 |
append_to_self(5) |
ArgumentError: The left-shift operator for this object doesn't do an append. ... |
ArgumentError: The left-shift operator for this object doesn't do an append. from (irb):26:in `append_to_self' from (irb):34 |
5 << 5 |
160 | 160 |
module ShiftMeansAppend def <<(x) end end class String include ShiftMeansAppend end class Array include ShiftMeansAppend end def append_to_self(x) unless x.is_a? ShiftMeansAppend raise ArgumentError, "I can't trust this object's left-shift operator." end x << x end append_to_self 4 |
ArgumentError: I can't trust this object's left-shift operator. ... |
ArgumentError: I can't trust this object's left-shift operator. from (irb):48:in `append_to_self' from (irb):52 |
append_to_self '4' |
"44" | "44" |