Reputation: 28592
Why doesn't Ruby have an is_an?
method? For example:
[].is_an? Array?
Upvotes: 3
Views: 1172
Reputation: 47551
is_an?
alias initializer.We ended up creating a little initializer to add this alias into our project, since we are mostly English speakers and prefer the grammatical correctness that is_an?
affords:
config/initializers/is_an.rb
# Aliases `is_an?` to `is_a?` so we correctly use `is_an?( Array )` instead of `is_a?( Array )`
#
# Surprisingly, not in the Ruby core, but it almost made it into the Rails core.
# For the discussion, see https://github.com/rails/rails/pull/6243
#
class Object
alias is_an? is_a?
end
Upvotes: 2
Reputation: 1151
Rails maintainer Andrew White gave the following reason:
it's purely an English language idiosyncrasy and since many (most?) Ruby programmers do not have English as their primary language they may not be aware of it, leading to confusion.
See this pull request for the full discussion on the topic: Improved readability for object introspection (is_a?)
Maybe it would get added if someone opened the same pull request in the Ruby repository!
Upvotes: 3
Reputation: 20869
You might consider using is_a?
instead eg.:
if [].is_a? Array
puts "Array"
end
If you really need to have is_an?
you might also achieve it with an alias:
class Object
alias :is_an? :is_a?
end
if [].is_an? Array
puts "Array"
end
Upvotes: 7
Reputation: 37324
I think the method you are looking for is kind_of?
See here:
http://ruby-doc.org/core-1.9.3/Object.html#method-i-kind_of-3F
Upvotes: 5
Reputation: 87476
Ruby has is_a?
already which does what you want. You could make is_an?
in 3 lines of code if you want but I think it's a bad idea. There are too many synonyms already in the standard Ruby library that make it harder than necessary to learn. I just recently learned that Enumerable's map and collect are the same.
Upvotes: 2