Understanding private methods in Ruby

前端 未结 7 1214
终归单人心
终归单人心 2020-11-29 00:01
class Example
 private
 def example_test
  puts \'Hello\'
 end
end

e = Example.new
e.example_test

This of course will not work, because we specifi

7条回答
  •  借酒劲吻你
    2020-11-29 00:43

    Does not exactly answer the Question, but you can call private methods this way

    class Example
     private
     def example_test
      puts 'Hello'
     end
    end
    
    e = Example.new
    e.send(:example_test)
    

提交回复
热议问题