{Josh Rendek}

<3 Ruby & Go

Preventing a Ruby Class From Being Reopened

I saw the question of “How can I prevent a class from being reopened again in Ruby?” pop up on the Ruby mailing list. While this is somewhat against the nature of ruby, it can be accomplished:

1
2
3
4
5
6
7
8
9
10
11
class Foo
  def Foo.method_added(name)
    raise "This class is closed for modification"
  end
end

class Foo
  def testing
    p "test"
  end
end

This will raise an exception anytime someone tries to reopen the class.

Comments