views:

80

answers:

4

In Java, you can do instanceof. Is there a Ruby equivalent?

+6  A: 

It's almost exactly the same. You can use Object's instance_of? method:

"a".instance_of?(String) # => true
John Topley
N.B. As far as I know, this returns false if `self` is an instance of a subclass of the argument.
Steven Xu
+1  A: 

I've had success with klass, which returns the class object. This seems to be Rails-specific.

Sample usage:

class Foo
end

Foo.new.klass
# => Foo

Foo.new.klass == Foo
# => true

Foo.new.klass == "Foo"
# => false

There is also a method that accomplishes this: Object.is_a?, which takes the class object as an argument and returns true if self is an instance of the class or an instance of a subclass.

Steven Xu
There's a method `Object#class`: http://apidock.com/ruby/Object/class
Mladen Jablanović
I wish I could pick two correct answers. Array doesn't have klass as a method, but it does have instance_of but active_record abjects do have .klass, i think.
DerNalia
`Array.class` should work just as well. I got mixed up in my answer because the library I'm using extends class `Object` with method `class`.
Steven Xu
+3  A: 

Have look at instance_of? and kind_of? methods. Here's the doc link http://ruby-doc.org/core/classes/Object.html#M000372

Anand
I would try is_a? as well.
Jason Noble
A: 

In Ruby, variables aren't objects, therefore they aren't instances of any classes und thus it doesn't make sense to check whether they are instances of any specific class.

Note that the same applies to Java, too: instanceof does not, as you claim, check if a variable is an instance of a class. It checks whether the object that the variable points to is an instance of a class. That is something completely different.

Jörg W Mittag
What the f**k? "In Ruby, everything is an object." - http://www.ruby-lang.org/en/about/
Webbisshh
@Webbisshh: Except, of course, all the things which *aren't* objects: variables, methods, messages, keywords, syntax, … If you so violently disagree that you even have to resort to swearing, why don't you simply show an example of treating a variable as an object? I would love to give an example myself, but it's kind of hard to find an example of something that doesn't exist …
Jörg W Mittag
@Jorg: I've just asked about "everything is an object" at [Is everything an object in ruby?](http://stackoverflow.com/questions/3429553/is-everything-an-object-in-ruby)
Andrew Grimm
@Jörg W Mittag: ok lets say y=10.Now do u agree that y is a variable? yes?Then let me tell you that y is an object as well because y.kind_of? = Fixnum.So a variable is nothing but whatever is on the right side.right?And ya, by everything we shouldn't consider statements. :D
Webbisshh
@Webbisshh: What you have shown is that the object that the variable `y` *points to* (the number `10`) is an object. But that's not the issue here. I never claimed that numbers aren't objects in Ruby. The question was whether variables are objects in Ruby.
Jörg W Mittag