[Ruby] About Ruby instance methods

less than 1 minute read

I write it as a complement to ambiguous knowledge about instance methods. If you have any mistakes, I would appreciate it if you could comment.

Instance method

  • When a method is defined in the class definition expression, it becomes an instance method. (Modified to syntax → definition formula)
  • Can be called for an instance of the class.
class Car
# Define instance method (here name method)
  def name
    "yellow cab"
  end
end
# Create an instance with class name.new and assign to variable taxi
taxi = Car.new
# Call instance method
taxi.name #=> "yellow cab"

Summary

  • The instance method is defined in the class.
  • Call an instance method after creating an instance.
  • The instance method can be called by “instance.method”.

Referenced links and books

・Method, class method and instance method were ambiguous https://qiita.com/right1121/items/c74d350bab32113d4f3d ・[Ruby] I learned again about classes and instances https://qiita.com/ksk_diffeasy/items/734b4961e9e6c7480d33 ・”Introduction to Ruby for Professionals” by Junichi Ito [Author] https://ruby-book.jnito.com/