Feature #20793
closedAllow Multiple Arguments for the .is_a? Method
Description
I propose allowing multiple arguments to be passed to the .is_a? Method imply "OR" semantics:
name.is_a? String, Symbol
Currently, we need to write the following to achieve the same functionality:
[String, Symbol].include?(name.class)
Updated by nobu (Nobuyoshi Nakada) 2 months ago
Also kind_of?
method?
Updated by Eregon (Benoit Daloze) 2 months ago · Edited
I think pattern matching should be used here instead of making is_a?
more complicated:
irb(main):001:0> name = :abc
=> :abc
irb(main):002:0> name in String | Symbol
=> true
irb(main):003:0> name = 42
=> 42
irb(main):004:0> name in String | Symbol
=> false
is_a?
is deeply optimized so accepting multiple arguments would likely make it slower or complicate things significantly.
Updated by artemb (Artem Borodkin) 2 months ago · Edited
Eregon (Benoit Daloze) wrote in #note-2:
I think pattern matching should be used here instead
Very nice, thank you!
If kind_of? (and alias is_a?) has a strong implementation optimization, I agree that this is not so important enhancement.
Updated by matz (Yukihiro Matsumoto) about 2 months ago
- Status changed from Open to Rejected
is_a?(A, B)
can be read as is_a?(A) || is_a?(B)
or is_a?(A) && is_a?(B)
. This ambiguity might lead to less readability.
Matz.