Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

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

Merged
merged 1 commit into from

2 participants

@kachick

Is this expected behavior?

ruby 1.9.3p194 (2012-04-20 revision 35410) [i686-linux]

p Slop::Commands.new {||p __id__}.__id__  #=> same
p Slop::Commands.new {|_|p __id__}.__id__ #=> differrent
@leejarvis leejarvis merged commit f16c105 into from
@leejarvis
Owner

Looks good, thanks!

@kachick kachick referenced this pull request from a commit in kachick/slop
@kachick kachick fix block-scope of "Slop.new" and other constructors(e.g: Slop.parse)…
…, these are influenced by arity number. this problem is same as #81
89215e4
@leejarvis leejarvis referenced this pull request from a commit
@leejarvis Revert "fix block-scope of "Slop.new" and other constructors(e.g: Slo…
…p.parse), these are influenced by arity number. this problem is same as #81"

This reverts commit 89215e4.

Conflicts:
	test/slop_test.rb
2c60751
@kachick kachick referenced this pull request from a commit in kachick/slop
@kachick kachick Revert "fix block-scope of "Slop::Commands#new" is influenced by arit…
…y number"

This reverts commit dafe660.
(issue/#81)
88c2407
@kachick kachick deleted the branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
Showing with 24 additions and 2 deletions.
  1. +10 −2 lib/slop/commands.rb
  2. +14 −0 test/commands_test.rb
View
12 lib/slop/commands.rb
@@ -40,7 +40,15 @@ def initialize(config = {}, &block)
@triggered_command = nil
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
@@ -194,4 +202,4 @@ def execute_global_opts(items, bang)
end
end
-end
+end
View
14 test/commands_test.rb
@@ -99,4 +99,18 @@ def setup
assert_equal %w( file1 file2 ), @commands.arguments
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
Something went wrong with that request. Please try again.