How can I access the class method and instance method in ruby?
In the first sentence you create anonymous class with superclass of String
:
my_str.class.superclass # => String
But this is not the essence of your actual question :)
Instance is an object of some class: String.new() # creates instance of class String
. Instances have classes (String.new()).class #=> String
. All classes are actually instances of a class Class
: String.class # => Class
. Instances of Class
class also have superclass
- class that they inherit from.
Instance method is a method that you can call on instance of an object.
"st ri ng".split # split is an instance method of String class
Class method in Ruby is a common term for instance methods of an object of Class
class (any class).
String.try_convert("abc") # try_convert is a class method of String class.
You can read more about instance and class methods in this article.