-
DRY Much?
I’ve been looking through the source of ActiveSupport and Rails in general, and I’ve noticed this particularly verbose style of monkeypatching a multitude of objects with the same behaviour:
# Can you safely .dup this object? # False for nil, false, true, symbols, numbers, class and module objects; true otherwise. class NilClass #:nodoc: def duplicable? false end end class FalseClass #:nodoc: def duplicable? false end end class TrueClass #:nodoc: def duplicable? false end end class Symbol #:nodoc: def duplicable? false end end class Numeric #:nodoc: def duplicable? false end end class Class #:nodoc: def duplicable? false end end class Module #:nodoc: def duplicable? false end endIt’s definitely not DRY, which is a key tenet of the Rails way. So why not do the same thing like this?
[NilClass, FalseClass, TrueClass, Symbol, Numeric, Class, Module].each { |klass| klass.class_eval { def duplicable?() false end } } # Or even… [NilClass, FalseClass, TrueClass, Symbol, Numeric, Class, Module].each do |klass| klass.class_eval { def duplicable?() false end } endDon’t give me “It’s less readable” because it’s not my friend, (well it is if you’re reading on the Tumblr Dashboard, but look here for better formatting) especially to the sort of person who would be going into source code to look around. So what other reasons can you come up with?
Fri26Mar