call a method on an instance of a class in ruby

To call a method on an instance of a class in Ruby, you first need to create an instance of the class. Once you have an instance of the class, you can call methods on that instance using the dot notation.

Here's an example:

main.rb
class MyClass
  def my_method
    puts "I am a method on an instance of MyClass!"
  end
end

my_instance = MyClass.new
my_instance.my_method
141 chars
9 lines

In this example, we define a class MyClass with an instance method my_method. We then create an instance of the class with MyClass.new and store it in a variable my_instance. Finally, we call the my_method method on the my_instance instance using the dot notation, like so: my_instance.my_method.

This will output "I am a method on an instance of MyClass!" to the console.

gistlibby LogSnag