20

Can I create a private instance method that can be called by a class method?

class Foo
  def initialize(n)
    @n = n
  end
  private # or protected?
  def plus(n)
    @n += n
  end
end

class Foo
  def Foo.bar(my_instance, n)
    my_instance.plus(n)
  end
end

a = Foo.new(5)
a.plus(3) # This should not be allowed, but
Foo.bar(a, 3) # I want to allow this

Apologies if this is a pretty elementary question, but I haven't been able to Google my way to a solution.

2
  • You should fix your question, you have a typo. Are the methods called bar or plus? Commented Jan 7, 2009 at 15:54
  • you're right - will fix. thanks. Commented Jan 7, 2009 at 15:57

3 Answers 3

20

Using private or protected really don't do that much in Ruby. You can call send on any object and use any method it has.

class Foo
  def Foo.bar(my_instance, n)
    my_instance.send(:plus, n)
  end
end
Sign up to request clarification or add additional context in comments.

Comments

10

You can do it as Samuel showed, but it is really bypassing the OO checks...

In Ruby you can send private methods only on the same object and protected only to objects of the same class. Static methods reside in a meta class, so they are in a different object (and also a different class) - so you cannot do as you would like using either private or protected.

Comments

8

You could also use instance_eval

class Foo
  def self.bar(my_instance, n)
    my_instance.instance_eval { plus(n) }
  end
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.