1

So a fairly common pattern I've run up against is something like this:

[:offer, :message].include? message.message_type

The inversion of wording there messes me up. So I wrote this little monkey patch for Symbol in specific.

def in? *scope
  scope.include? self
end

So now I can do the previous this way:

message.message_type.in? :offer, :message

This works fine and I'm happy with it, but occasionally I need similar functionality for other objects. Model objects in Rails apps being the most common case but strings occasionally, etc.

What kind of issues would I run into if I monkey patched this directly into Object?

Drew
  • 15,158
  • 17
  • 68
  • 77

1 Answers1

2

Rails (ActiveSupport) already patches Object with this method. Here is the documentation: http://api.rubyonrails.org/classes/Object.html#method-i-in-3F.

Returns true if this object is included in the argument. Argument must be any object which responds to #include?. Usage:

characters = ["Konata", "Kagami", "Tsukasa"]
"Konata".in?(characters) # => true    

This will throw an ArgumentError if the argument doesn’t respond to #include?.

Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367