add a static method to a class in ruby

To add a static method to a class in Ruby, you can define a method on the class itself using the self keyword. Here's an example:

main.rb
class MyClass
  def self.my_static_method
    # code for the static method goes here
  end
end
95 chars
6 lines

In this example, we define a static method called my_static_method on the MyClass class. The self keyword before the method name indicates that it is a static method.

To call the static method, you don't need to create an instance of the class. You can just call the method on the class itself:

main.rb
MyClass.my_static_method
25 chars
2 lines

This will call the my_static_method method on the MyClass class.

gistlibby LogSnag