<p>Other than <code>self.class.send :method, args...</code>, of course.  I'd like to make a rather complex method available at both the class and instance level without duplicating the code.</p>

<hr>

<p><strong>UPDATE:</strong>  </p>

<p>@Jonathan Branam: that was my assumption, but I wanted to make sure nobody else had found a way around.  Visibility in Ruby is very different from that in Java.  You're also quite right that <code>private</code> doesn't work on class methods, though this will declare a private class method:</p>

<pre><code>class Foo
  class &lt;&lt;self
    private
    def bar
      puts 'bar'
    end
  end
end

Foo.bar
# =&gt; NoMethodError: private method 'bar' called for Foo:Class
</code></pre>

[{'answer_id': 20690, 'author': 'John', 'author_id': 2168, 'author_profile': 'https://Stackoverflow.com/users/2168', 'pm_score': -1, 'selected': False, 'text': '<p>Unless I\'m misunderstanding, don\'t you just need something like this:</p>\n\n<pre><code>class Foo\n    private\n    def Foo.bar\n        # Complex logic goes here\n        puts "hi"\n    end\n\n    public\n    def bar\n        Foo.bar\n    end\nend\n</code></pre>\n\n<p>Of course you could change the second definition to use your self.class.send approach if you wanted to avoid hardcoding the class name... </p>\n'}, {'answer_id': 20746, 'author': 'Jonathan Branam', 'author_id': 2347, 'author_profile': 'https://Stackoverflow.com/users/2347', 'pm_score': 5, 'selected': True, 'text': '<p>Here is a code snippet to go along with the question. Using "private" in a class definition does not apply to class methods. You need to use "private_class_method" as in the following example.</p>\n\n<pre><code>class Foo\n  def self.private_bar\n    # Complex logic goes here\n    puts "hi"\n  end\n  private_class_method :private_bar\n  class &lt;&lt;self\n    private\n    def another_private_bar\n      puts "bar"\n    end\n  end\n  public\n  def instance_bar\n    self.class.private_bar\n  end\n  def instance_bar2\n    self.class.another_private_bar\n  end\nend\n\nf=Foo.new\nf=instance_bar # NoMethodError: private method `private_bar\' called for Foo:Class\nf=instance_bar2 # NoMethodError: private method `another_private_bar\' called for Foo:Class\n</code></pre>\n\n<p>I don\'t see a way to get around this. The documentation says that you cannot specify the receive of a private method. Also you can only access a private method from the same instance. The class Foo is a different object than a given instance of Foo.</p>\n\n<p>Don\'t take my answer as final. I\'m certainly not an expert, but I wanted to provide a code snippet so that others who attempt to answer will have properly private class methods.</p>\n'}, {'answer_id': 8543280, 'author': 'Andrew Grimm', 'author_id': 38765, 'author_profile': 'https://Stackoverflow.com/users/38765', 'pm_score': 0, 'selected': False, 'text': '<p>If your method is merely a <a href="https://github.com/kevinrutherford/reek/wiki/utility-function" rel="nofollow">utility function</a> (that is, it doesn\'t rely on any instance variables), you could put the method into a module and <code>include</code> and <code>extend</code> the class so that it\'s available as both a private class method and a private instance method.</p>\n'}, {'answer_id': 11322794, 'author': 'magicgregz', 'author_id': 1169468, 'author_profile': 'https://Stackoverflow.com/users/1169468', 'pm_score': 0, 'selected': False, 'text': '<p>This is the way to play with "real" private class methods.</p>\n\n<pre><code>class Foo\n  def self.private_bar\n    # Complex logic goes here\n    puts "hi"\n  end\n  private_class_method :private_bar\n  class &lt;&lt;self\n    private\n    def another_private_bar\n      puts "bar"\n    end\n  end\n  public\n  def instance_bar\n    self.class.private_bar\n  end\n  def instance_bar2\n    self.class.another_private_bar\n  end\n  def calling_private_method\n    Foo.send :another_private_bar\n    self.class.send :private_bar\n  end\nend\nf=Foo.new\nf.send :calling_private_method \n # "bar"\n # "hi"\nFoo.send :another_private_bar\n# "bar"\n</code></pre>\n\n<p>cheers</p>\n'}, {'answer_id': 24224054, 'author': 'Alexey', 'author_id': 898649, 'author_profile': 'https://Stackoverflow.com/users/898649', 'pm_score': 3, 'selected': False, 'text': '<p>Let me contribute to this list of more or less strange solutions and non-solutions:</p>\n\n<pre class="lang-rb prettyprint-override"><code>puts RUBY_VERSION # =&gt; 2.1.2\n\nclass C\n  class &lt;&lt; self\n    private def foo\n      \'Je suis foo\'\n    end\n  end\n\n  private define_method :foo, &amp;method(:foo)\n\n  def bar\n    foo\n  end\nend\n\nputs C.new.bar # =&gt; Je suis foo\nputs C.new.foo # =&gt; NoMethodError\n</code></pre>\n'}, {'answer_id': 35921805, 'author': 'Michael Krupp', 'author_id': 1986344, 'author_profile': 'https://Stackoverflow.com/users/1986344', 'pm_score': 2, 'selected': False, 'text': '<p>Nowadays you don\'t need the helper methods anymore. You can simply inline them with your method definition. This should feel very familiar to the Java folks:</p>\n\n<pre><code>class MyClass\n\n  private_class_method def self.my_private_method\n    puts "private class method"\n  end\n\n  private def my_private_method\n    puts "private instance method"\n  end\n\nend\n</code></pre>\n\n<p>And no, you cannot call a private class method from an instance method. However, you could instead implement the the <strong>private</strong> class method as <strong>public</strong> class method in a <strong>private</strong> nested class instead, using the <code>private_constant</code> helper method. See <a href="https://blog.arkency.com/2016/02/private-classes-in-ruby/" rel="nofollow">this blogpost</a> for more detail.</p>\n'}, {'answer_id': 55486235, 'author': 'Kache', 'author_id': 234593, 'author_profile': 'https://Stackoverflow.com/users/234593', 'pm_score': 0, 'selected': False, 'text': '<p>This is probably the most "native vanilla Ruby" way:</p>\n\n<pre><code>class Foo\n  module PrivateStatic # like Java\n    private def foo\n      \'foo\'\n    end\n  end\n  extend PrivateStatic\n  include PrivateStatic\n\n  def self.static_public_call\n    "static public #{foo}"\n  end\n\n  def public_call\n    "instance public #{foo}"\n  end\nend\n\nFoo.static_public_call # \'static public foo\'\nFoo.new.public_call # \'instance public foo\'\nFoo.foo # NoMethodError: private method `foo\' called for Foo:Class\nFoo.new.foo # NoMethodError: private method `foo\' called for #&lt;Foo:0x00007fa154d13f10&gt;\n</code></pre>\n\n<p>With some Ruby metaprogramming, you could even make it look like:</p>\n\n<pre><code>class Foo\n  def self.foo\n    \'foo\'\n  end\n\n  extend PrivateStatic\n  private_static :foo\nend\n</code></pre>\n\n<p>Ruby\'s metaprogramming is quite powerful, so you could technically implement any scoping rules you might want. That being said, I\'d still prefer the clarity and <a href="https://en.wikipedia.org/wiki/Principle_of_least_astonishment" rel="nofollow noreferrer">minimal surprise</a> of the first variant.</p>\n'}]
['https://Stackoverflow.com/questions/20674', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1190/']