Is it possible to freeze (make immutable) a class or a method in Ruby?

Viewed 69

In Ruby:

module Example
  class ExampleValidator
    def mymethod
      # do something important
    end
  end
end

Is it possible to make mymethod immutable (i.e. no monkey patching)?

Otherwise it would also be acceptable to "freeze" the entire class ExampleValidator.

1 Answers

Yes, you can call freeze on a Module.

Example::ExampleVaildator.freeze

However, I'd question why this is necessary. Perhaps it is solving a social problem with a technical one. A tool such as Rubocop should warn about monkey patches.

Related