我如何访问私有方法

Dip*_*hal 0 ruby ruby-on-rails ruby-on-rails-3.1

base.rb

module Search
  class Base

    attr_accessor :query_model
    attr_accessor :result

    def initialize(query_obj)
      self.query_model = query_obj
    end

    def execute         
    end

  end
end
Run Code Online (Sandbox Code Playgroud)

social_mention.rb

module Search
  class SocialMention < Base

    def execute
      self.result = RestClient.get api_client, :params => query_params
      parse_result
    end

    private

    def api_client
      'http://socialmention.com/search'
    end
end
Run Code Online (Sandbox Code Playgroud)

如何访问api_client方法?

我正在关注这个但没有访问它.

Search::SocialMention.new(query).api_client
Run Code Online (Sandbox Code Playgroud)

访问私有方法的正确方法是什么?

谢谢

xda*_*azz 5

您可以通过以下Object#send方法对私有方法进行外部访问:

Search::SocialMention.new(query).send(:api_client)
Run Code Online (Sandbox Code Playgroud)

但既然你想在外面访问它,为什么一开始就把它作为私有?