Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I dynamically define a method as private?

This does not seem to work:

class Test
  private

  define_method :private_method do 
    "uh!"
  end
end

puts Test.new.private_method
like image 818
knoopx Avatar asked Sep 23 '10 19:09

knoopx


People also ask

How do I make my class methods private?

The classic way to make class methods private is to open the eigenclass and use the private keyword on the instance methods of the eigenclass — which is what you commonly refer to as class methods.

What does public_ send do in Ruby?

Use the public_send method when you want to call methods and properties dynamically on other objects. This ensures that you are not calling any private methods or properties.

What is send method in Ruby?

Ruby Language Metaprogramming send() method send() is used to pass message to object . send() is an instance method of the Object class. The first argument in send() is the message that you're sending to the object - that is, the name of a method. It could be string or symbol but symbols are preferred.


1 Answers

Test.instance_eval { private :private_method }

Or, just run

private :private_method

from within the Test class.

like image 189
Brian Campbell Avatar answered Oct 13 '22 20:10

Brian Campbell