0
votes

I could not find usage about in here https://docs.ruby-lang.org/en/master/index.html, but it seems to be supported.

h = {a: 1}
:a in h # => true

2 in 1..3 # => true

How does it work? Is there any special method I can override like == or [] ?

2

2 Answers

0
votes

I think Ruby 3.0's in is what you are looking for.

0
votes

Please see here:

https://docs.ruby-lang.org/en/master/syntax/pattern_matching_rdoc.html

<expression> in <pattern> is the same as case <expression>; in <pattern>; true; else false; end

So :a in h is the same as

case :a
in h
  true
else
  false
end

You can use it when you only want to know if a pattern has been matched or not