Update: Immutable, With Exceptions

This is an update to my previous article: Making methods immutable in Ruby.

By popular demand, I’ve made my Immutable module raise an exception, by default, if you try to override an immutable method.

So, the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
require 'rubygems'
require 'immutable'

module Foo
  include Immutable

  def foo
    :foo
  end

  immutable_method :foo
end

module Foo
  def foo
    :baz
  end
end

Will raise an error:

1
Cannot override the immutable method: foo (Immutable::CannotOverrideMethod)

Comments