# Is it possible to turn a private method into a public one?

**URL:** <https://www.ruby-forum.com/t/is-it-possible-to-turn-a-private-method-into-a-public-one/147841>\
**Category:** Ruby\
**Created:** [September 5, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-turn-a-private-method-into-a-public-one/147841 "2008-09-05T23:12:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lpc2148](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lpc2148](https://www.ruby-forum.com/u/lpc2148)\
**Post date:** [September 5, 2008, 11:12pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-turn-a-private-method-into-a-public-one/147841/1 "2008-09-05T23:12:38Z")

</div>

hey guys !

could a private/protected method be turned into a public one ?

---

<div class="post-metadata">

**Author:** ![lpc2148](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lpc2148](https://www.ruby-forum.com/u/lpc2148)\
**Post date:** [September 5, 2008, 11:34pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-turn-a-private-method-into-a-public-one/147841/2 "2008-09-05T23:34:56Z")

</div>

Lex W. wrote:

> hey guys !
> 
> could a private/protected method be turned into a public one ?

class K  
private  
def m  
end

# now m is private

public :m

# and now it is public

end

TPR.

---

<div class="post-metadata">

**Author:** ![lpc2148](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lpc2148](https://www.ruby-forum.com/u/lpc2148)\
**Post date:** [September 5, 2008, 11:44pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-turn-a-private-method-into-a-public-one/147841/3 "2008-09-05T23:44:27Z")

</div>

Thomas B. wrote:

> Lex W. wrote:
> 
> > hey guys !
> > 
> > could a private/protected method be turned into a public one ?
> 
> class K  
> private  
> def m  
> end
> 
> # now m is private
> 
> public :m
> 
> # and now it is public
> 
> end
> 
> TPR.

Thank you ! This will come in handy !

---

<div class="post-metadata">

**Author:** ![lpc2148](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lpc2148](https://www.ruby-forum.com/u/lpc2148)\
**Post date:** [September 6, 2008, 12:28pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-turn-a-private-method-into-a-public-one/147841/4 "2008-09-06T12:28:02Z")

</div>

Lex W. wrote:

> Thank you ! This will come in handy !

Also, it’s possible to bypass the protection using the Object#send  
method. This is useful for unit-testing private methods.

---

<div class="post-metadata">

**Author:** ![lpc2148](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/9de053/32.png) [@lpc2148](https://www.ruby-forum.com/u/lpc2148)\
**Post date:** [September 6, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/is-it-possible-to-turn-a-private-method-into-a-public-one/147841/5 "2008-09-06T17:44:10Z")

</div>

On Fri, Sep 5, 2008 at 11:06 PM, Lex W. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> could a private/protected method be turned into a public one ?

Sure.

On the one hand you can circumvect access control with Object#send:

object.send(:method\_name\_even\_if\_private, …)

And yes, you indeed can change the visibility:

```
class C
  private
  def foo
    puts "it works"
  end
end

c = C.new
begin
  c.foo
rescue
  puts $!
  C.send(:public, :foo)
  c.foo
end
# =>
# private method `foo' called for #<C:0x22013c>
# it works

```

That public is similar to the private call in C, only done out of the  
class and via #send because it is private.

It is interesting to note that we’ve been able to call the method on  
an object that was created when #foo was private, as the raised  
exception proves. That’s because method calls are resolved on a per  
call basis due to the dynamic nature of Ruby.
