Project

General

Profile

Actions

Feature #20793

open

Allow Multiple Arguments for the .is_a? Method

Added by artemb (Artem Borodkin) 26 days ago. Updated 26 days ago.

Status:
Open
Assignee:
-
Target version:
-
[ruby-core:119502]

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) 26 days ago

Also kind_of? method?

Updated by Eregon (Benoit Daloze) 26 days 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) 26 days 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.

Actions

Also available in: Atom PDF

Like0
Like0Like4Like0