lib/hobo_support/methodcall.rb in hobosupport-0.9.0 vs lib/hobo_support/methodcall.rb in hobosupport-0.9.100
- old
+ new
@@ -54,12 +54,35 @@
class SafeNil
include Singleton
+ DONT_REDEFINE_METHODS = "__id__", "__send__", "object_id"
+
+ NIL_RESPONSE_METHODS = ["to_s", "to_json", "to_yaml", "__id__", "__is_a__", "__metaclass__", "__send__"]
+
+ (NIL_RESPONSE_METHODS - DONT_REDEFINE_METHODS).each do |method|
+ # can't use define_method with a block
+ eval "
+ def #{method}(*args, &b)
+ nil.send(:#{method}, *args, &b)
+ end"
+ end
+
+ (instance_methods.map{|m| m.to_s} - NIL_RESPONSE_METHODS - DONT_REDEFINE_METHODS).each do |method|
+ # can't use define_method with a block
+ eval "
+ def #{method}(*args, &b)
+ nil
+ end"
+ end
+
+ def to_s
+ ""
+ end
+
def method_missing(method, *args, &b)
- return nil unless nil.respond_to? method
- nil.send(method, *args, &b) rescue nil
+ return nil
end
end
alias DelegateClass_without_safe_nil DelegateClass