Undefining a Method : remove_method « Method « Ruby






Undefining a Method


class RandomizingArray < Array
  def <<(e)
    insert(rand(size), e)
  end

  def [](i)
    super(rand(size))
  end
end

a = RandomizingArray.new
a << 1 << 2 << 3 << 4 << 5 << 6           # => [6, 3, 4, 5, 2, 1]

p a[0]
p a[0]
p a[0]
p

class RandomizingArray
  remove_method('[]')
end

a[0]                                      # => 6
a[0]                                      # => 6
a[0]                                      # => 6

 








Related examples in the same category

1.Remove a method for an intance
2.Remove a new added method after calling