1

Bothered by that we could test in Ruby whether an array (or another Enumerable) contains a value with the include? method...

puts %w(one two three four).include?("two")
# => true
puts %w(one two three four).include?("five")
# => false

... but not directly test whether a value is included in an array (semantically identical, but not part of the language)...

puts "two".in?( %w(one two three four) )
puts "five".in?( %w(one two three four) )

I thought about monkey patching Object to have a handy Object#in? method, something similar to:

class Object
  def in?(array)
    return false unless array.respond_to?(:include?)  # Or perhaps raise an error.
    return array.include?(self)
  end
end

With that method definition on Object, things like

"two".in?( %w(one two three four) )
# => true
:nuts.in?( [:banana, :chocolate, :pie] )
# => false

work nicely, and are very close to a normal sentence expressing that test.

Frequently, I run into that if element.in?(array) is a subtly more natural expression than if array.include?(element), especially if the array is included as a literal (instead of a variable reference).

Question (while neglecting the general opinion on monkey patching): is this example of monkey patching a smart thing to do? What are specific drawbacks for having an Object#in? method, again not going into the generic pros and cons of monkey patching objects (in Ruby)?

3
  • Oh, I just discover that Ruby on Rails does something similar, adding #in? to Object. So, I'm alone with this goal about striving for code to be close to a normal natural expression in English. Jan 9, 2019 at 21:10
  • 1
    I would recommend as a minimum change calling the method included_in? this way there is a simple implication that the object being passed should implement include. Jan 9, 2019 at 22:01
  • @engineersmnky, I understand, and I like the consequential implication ... but it's just not how I verbalize the statement. In my head, I think "is this value in this array?" (and not "is this value included in this array?") but YMMV. Jan 10, 2019 at 11:00

2 Answers 2

3

Ignoring the general arguments for/against monkey patching in Ruby, the code you describe is an example of a convenience method to help with your readability. Arguments against its inclusion also apply to the generic case as well, so there doesn't appear to be a specific drawback. Technically, this method is already included in the Ruby on Rails framework, so the authors shared your view in supporting it as a natural expression.

1
  • Accepting this answer, as it addresses (or at least: mentions) the point of a potential drawback of monkey patching, in the light of the original question. Jan 9, 2019 at 21:33
2

I would either require the appropriate Rails Active Support module (core_ext/object/inclusion.rb) into your project, or if you are going to modify Object yourself, use the same source code as Rails' Object#in? method.

# File activesupport/lib/active_support/core_ext/object/inclusion.rb, line 12
def in?(another_object)
  another_object.include?(self)
rescue NoMethodError
  raise ArgumentError.new("The parameter passed to #in? must respond to #include?")
end
2
  • Yeah, I saw that code snippet as well. At least using the same code (especially in handling an argument error - an object that doesn't respond to include?) conforms to a common-ish opinion. Jan 9, 2019 at 21:26
  • Oh, for a non-web application in Ruby - yes, it happens - I'd not opt quickly for requiring Rails modules in my project. Then I'd favor to just monkey patch Object myself (but using the similar code, decreasing the chance of confusion). Jan 9, 2019 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.