Skip to content

Instantly share code, notes, and snippets.

@daveworth
Created November 19, 2013 00:35
Show Gist options
  • Save daveworth/7538060 to your computer and use it in GitHub Desktop.
Save daveworth/7538060 to your computer and use it in GitHub Desktop.
hoping to help a friend debug what method exactly is called on a proc/lambda when passed to an enumerator..
require 'spec_helper'
describe "First Class Objects are Weird" do
let(:a_lambda) { lambda { |p| puts ["Lambda", p].join(" ") } }
let(:a_proc) { proc { |p| puts ["Proc", p].join(" ") } }
context "wat" do
pending "totally does not work" do
foo = double # was hoping to find out what method was called on this double... it doesn't work
%w(a).each &foo
end
end
context "lambdas" do
it "should #call directly" do
a_lambda.should_receive(:call).exactly(1).times.and_call_original
%w(a).each &a_lambda
end
it "might call :[]" do
a_lambda.should_receive(:[]).exactly(1).times.and_call_original
%w(a).each &a_lambda
end
it "might call yield" do
a_lambda.should_receive(:yield).exactly(1).times.and_call_original
%w(a).each &a_lambda
end
it "can be intercepted with an explicit call to #call" do
a_lambda.should_receive(:call).exactly(1).times.and_call_original
a_lambda.call("foo")
end
end
context "procs" do
it 'should #call directly' do
a_proc.should_receive(:call).exactly(1).times.and_call_original
%w(a).each &a_proc
end
it "might call :[]" do
a_proc.should_receive(:[]).exactly(1).times.and_call_original
%w(a).each &a_proc
end
it "might call yield" do
a_proc.should_receive(:yield).exactly(1).times.and_call_original
%w(a).each &a_proc
end
it "can be intercepted with an explicit call to #call" do
a_proc.should_receive(:call).exactly(1).times.and_call_original
a_proc.call("foo")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment