0

I recently started learning ruby. I am confused between class methods, instance methods, instance variables, and class variables. I googled a lot, but I didn't get any clarification on those. Any help along with examples would be appreciated.

2
  • 1
    Huh. When I google something like "ruby what is an instance variable", I see tons of pertinent information. Commented Oct 6, 2016 at 18:50
  • 2
    Possible duplicate of Ruby class instance variable vs. class variable Commented Oct 6, 2016 at 18:55

2 Answers 2

2

First take a look at this diagram:

from "Metaprogramming Ruby" book

You can rightly say that “obj has a method called my_method( ),” meaning that you’re able to call obj.my_method(). By contrast, you shouldn’t say that “MyClass has a method named my_method().” That would be confusing, because it would imply that you’re able to call MyClass.my_method() as if it were a class method.

To remove the ambiguity, you should say that my_method() is an instance method (not just “a method”) of MyClass, meaning that it’s defined in MyClass, and you actually need an instance of MyClass to call it. It’s the same method, but when you talk about the class, you call it an instance method, and when you talk about the object, you simply call it a method. Remember this distinction, and you won’t get confused when writing introspective code like this:

String.instance_methods == "abc".methods # => true String.methods == "abc".methods # => false

an object’s instance variables live in the object itself, and an object’s methods live in the object’s class. That’s why objects of the same class share methods but don’t share instance variables.

Sign up to request clarification or add additional context in comments.

1 Comment

You can also take a look at this answer: stackoverflow.com/a/23423950/6296901
1

I am confused between class methods, instance methods,

There is no such thing as a "class method" in Ruby. There is exactly one kind of methods: instance methods.

Rubyists will sometimes talk about "class methods", but that is just a convenient name we use to refer to "instance methods of the singleton class of an instance of the Class class". That's quite a mouthful, and so we will abbreviate it to "class methods", but we know full well that class methods don't exist in the Ruby language.

instance variables, and class variables.

Really, the distinction is pretty much what it says on the tin: instance variables belong to objects (aka instances), whereas class variables belong to classes. Actually, class variables have pretty broad scope: a class variable is visible inside the class it is defined in, all of its instances, all of its subclasses, all instances of its subclasses, all of its subclasses' subclasses, all instances of all of its subclasses' subclasses and so on and so forth. Basically, class variables are visible through the entire class sub-hierarchy and all direct and indirect instances.

Note that classes are objects like any other, they are instances, too (of the Class class). Which means they can have instance variables as well, just like all other objects. 99% of the time, when you think you want a class variable, you actually want an instance variable of the class.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.