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.
Testing Whether an Object Is String-like | ||
---|---|---|
Code | Expected | Actual |
'A string'.respond_to? :to_str |
true | true |
Exception.new.respond_to? :to_str |
true | true |
4.respond_to? :to_str |
false | false |
def join_to_successor(s) raise ArgumentError, 'No successor method!' unless s.respond_to? :succ return "#{s}#{s.succ}" end join_to_successor('a') |
"ab" | "ab" |
join_to_successor(4) |
"45" | "45" |
join_to_successor(4.01) |
ArgumentError: No successor method! ... |
ArgumentError: No successor method! from (irb):5:in `join_to_successor' from (irb):10 |