0

I have the method

  def self.fetch(key, &block)
    begin
      Rails.cache.fetch(key, block)
    rescue Redis::CommandError => e
      raise unless e.message == "OOM command not allowed when used memory > 'maxmemory'."

      Utils.log_exception ex
      yield
    end
  end

This fails on line 3 with the error

ArgumentError: Missing block: Calling Cache#fetch with force: true requires a block.

I'm not sure if passing block in as a parameter like that is the correct way but I couldn't see another way.

What is the correct way to pass a variable containing a block to a method?

1
  • See the docs for Block Arguments (2nd example) Commented Apr 23, 2019 at 7:26

1 Answer 1

3

The & ampersand unary prefix operator in an argument list is used to "unroll" an instance of Proc as if it were passed as a block to a method, just like the & ampersand sigil in a parameter list is used to "roll up" a block passed as an argument into an instance of Proc:

Rails.cache.fetch(key, &block)
#                      ↑
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.