"with(&block)"在Ruby中意味着什么?

Kev*_*ell 1 ruby rubygems sidekiq

在我正在使用的宝石中,我找到了片段:

@object.with(&block)

但该方法with(&block)未在项目中定义.看起来它被定义为Ruby somwhere中的基本方法,但我不确定.

这是什么意思?有人可以指出定义该方法的位置(比如Object或Class或其他一些Ruby类)?

编辑:

有问题的代码:

  def self.redis(&block)
    raise ArgumentError, "requires a block" if !block
    @redis ||= Sidekiq::RedisConnection.create(@hash || {})
    @redis.with(&block)
  end
Run Code Online (Sandbox Code Playgroud)

它来自Sidekiq项目(https://github.com/mperham/sidekiq).该项目还包括redis-rbgem(https://github.com/redis/redis-rb).我找不到任何一种with方法.

也许我只是缺少一些东西.

Joh*_*ter 7

它被定义为sidekiq使用的connection_pool gem的一部分,它的源代码如下.看起来它的目的是从池中获取连接,将其提供给提供的块,然后将连接释放回池中.

这是我发现的方式:

 pry> redis = Sidekiq::RedisConnection.create({})
 pry> redis.method(:with).source

  def with
    conn = checkout
    begin
      yield conn
    ensure
      checkin
    end
  end

pry> redis.method(:with).source_location

["./ruby/gems/2.0.0/gems/connection_pool-1.1.0/lib/connection_pool.rb", 46]
Run Code Online (Sandbox Code Playgroud)

并确定依赖:

~$ bundle exec gem dependency connection_pool --reverse-dependencies

Gem connection_pool-1.1.0
  minitest (>= 5.0.0, development)
  Used by
    sidekiq-2.16.0 (connection_pool (>= 1.0.0))
Run Code Online (Sandbox Code Playgroud)