How to 'disable' a certain method in ruby from being called in the console

Viewed 1682

Let's say I have a class ABC with two methods.

class ABC
  def test
     "test"
  end

  def display_test
    puts test
  end
end

I only want to be able to call ABC.new.display_test from my console (IRB) (returning me 'test') and not be able to call ABC.new.test or ABC.new.send(:test) for that matter. Is this possible? If so, how?

4 Answers

You can use undef method to do disable any methods, but in this case, you can't call it anywhere.


class ABC
  def test
     "test"
  end

  def display_test
    puts test
  end

  undef :display_test
end

Related