user2490003
user2490003

Reputation: 11920

RSpec/Ruby - creating a proc inside a `let` block

I want to define a Proc to be passed around to various methods, and I wanted to define it in a let block

let(:proc) { Proc.new { |f| f.write("c1,c2,c3") } }

However I keep getting the error

ArgumentError: tried to create Proc object without a block

Although the Proc clearly has a block. When I execute the Proc by itself in IRB, it works, so I suspect it's something to do with how let initializes it.

Is there a way to work around this?

Thanks!

Upvotes: 1

Views: 1196

Answers (2)

Jeremie
Jeremie

Reputation: 2421

For anyone stumbling on this:

let(:proc) { ->(f) { f.write("c1,c2,c3") } }

should work

Upvotes: 1

zetetic
zetetic

Reputation: 47578

I'm not able to replicate this. Perhaps you have an older version of RSpec?

In any case, don't forget that proc is Ruby shorthand for Proc.new:

proc { "hello" }.call
# "hello"

proc
# ArgumentError

You may have simply run into a naming collision. Try changing the let argument name to something other than "proc".

Upvotes: 4

Related Questions