Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BlockScope of "Slop::Commands#new" is influenced by arity number #81

Merged
merged 1 commit into from Sep 16, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/slop/commands.rb
Expand Up @@ -40,7 +40,15 @@ def initialize(config = {}, &block)
@triggered_command = nil @triggered_command = nil


if block_given? if block_given?
block.arity == 1 ? yield(self) : instance_eval(&block) case block.arity.abs
when 0
instance_exec(&block)
when 1
instance_exec(self, &block)
else
raise ArgumentError,
"wrong number of block arguments (#{block.arity} for #{0..1})"
end
end end
end end


Expand Down Expand Up @@ -194,4 +202,4 @@ def execute_global_opts(items, bang)
end end


end end
end end
14 changes: 14 additions & 0 deletions test/commands_test.rb
Expand Up @@ -99,4 +99,18 @@ def setup
assert_equal %w( file1 file2 ), @commands.arguments assert_equal %w( file1 file2 ), @commands.arguments
end end


test "new with block scope" do
peep = nil
ret = Slop::Commands.new { peep = self }
assert_same ret, peep

peep = nil
ret = Slop::Commands.new { |a| peep = self }
assert_same ret, peep

assert_raises ArgumentError do
Slop::Commands.new { |a, b| }
end
end

end end