Can you call a private method outside a Ruby class using its object?

Technology CommunityCategory: RubyCan you call a private method outside a Ruby class using its object?
VietMX Staff asked 3 years ago

Yes, with the help of the send method.

Given the class Test:

class Test
   private
       def method
         p "I am a private method"
      end
end

We can execute the private method using send:

>> Test.new.send(:method)
"I am a private method"