如何制作受保护的单例方法

时间:2016-03-28 09:50:57

标签: ruby protected

如何使以下方法some_protected受保护或私有?它应该在没有继承的情况下实现。

module Sample
  def self.some_public
    some_protected
  end

  protected

  def self.some_protected
    puts 'Bingo!'
  end
end

Sample::some_public     # Bingo!
Sample::some_protected  # Bingo! (but expected an error that method is not accessible)

1 个答案:

答案 0 :(得分:2)

在单例类中工作可能是最简单的。

module Sample; end

class <<Sample
  def some_public
    some_protected
  end
  protected def some_protected
    puts 'Bingo!'
  end
end