Allow let to memoize similar methods in a single block by passing multiple arguments #574

Closed
wants to merge 1 commit into
from

Conversation

Projects
None yet
3 participants

p8 commented Feb 16, 2012

For some specs I want to instantiate similar but distinct objects.
But instead of doing:

let(:thing) do
   thing = Thing.new
   thing.do_something
   thing
end

let(:another_thing) do
   thing = Thing.new
   thing.do_something
   thing
end

I'd like to be able to do:

let(:thing, :another_thing) do
   thing = Thing.new
   thing.do_something
   thing
end
Owner

dchelimsky commented Feb 16, 2012

My instinct is to close this, but I'm going to leave it open for some discussion first.

The reason is that let comes from lisp, in which it is used to bind values to variable names within a scope. What you propose makes it feel more like a factory method, which is conceptually a different animal, and I think this would just make it harder for new users to understand what let means.

p8 commented Feb 16, 2012

That's a valid point.
Maybe I've been using let the wrong way (mostly as memoized factory methods) :)

Contributor

justinko commented Feb 16, 2012

Having multiple let occurrences of the same value is rare, IMO. I would resort to using a method:

def foo; :foo; end
let(:foo1) { foo }
let(:foo2) { foo }

Or iterate:

[:foo1, :foo2].each do |foo|
  let(foo) { the_repeated_value }
end
Owner

dchelimsky commented Feb 16, 2012

You're not doing anything wrong. Technically that's how it works, because Ruby lets us define methods and bind instance variables, but not bind local variables, but let should feel (subjective, I realize) like a local variable binding.

justinko closed this May 20, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment