add a method to a class in ruby

To add a method to a class in Ruby, you can use the define_method method. Here's an example:

main.rb
class MyClass
  define_method :my_method do |arg1, arg2|
    # method logic goes here
  end
end
96 chars
6 lines

This code defines a new method called my_method on the MyClass class. The define_method method takes the name of the new method as a symbol as its first argument, and a block of code that defines the method's logic as its second argument.

In this example, the block takes two arguments (arg1 and arg2), which can be used in the method's logic as needed.

gistlibby LogSnag