Permalink
Browse files

Fix superclass+dup mix

  • Loading branch information...
1 parent de6b9a4 commit 58ef2cbd3c762cffb23200cb4e6741afb09a4f8d @josevalim josevalim committed Jun 18, 2012
Showing with 25 additions and 2 deletions.
  1. +8 −1 lib/thor/base.rb
  2. +1 −1 lib/thor/version.rb
  3. +16 −0 spec/thor_spec.rb
View
@@ -592,7 +592,14 @@ def from_superclass(method, default=nil)
default
else
value = superclass.send(method)
- value.dup if value
+
+ if value
+ if value.is_a?(TrueClass) || value.is_a?(Symbol)
+ value
+ else
+ value.dup
+ end
+ end
@sferik
sferik Jun 18, 2012 Member

IMHO, this would be clearer as a case statement:

case value
when TrueClass, FalseClass, NilClass, Symbol
  value
else
  value.dup
end
@eventualbuddha
eventualbuddha Jun 18, 2012 Contributor

Why TrueClass et al? Why not simply this?

case value
when true, false, nil, Symbol
  value
else
  value.dup
end

Granted, I don't understand the problem this commit was trying to fix so I may be missing something.

@sferik
sferik Jun 18, 2012 Member

That would work too, given that TrueClass, FalseClass, and NilClass are all singletons. It's just a matter of style. I don't necessarily agree that your code is simpler than mine. In my example, all the comparisons are made against classes, while in your example, you compare value to both instances and classes. In my opinion, this has slightly higher cognitive overhead, but I'd prefer either of these implementations to the status quo.

end
end
View
@@ -1,3 +1,3 @@
class Thor
- VERSION = "0.15.2"
+ VERSION = "0.15.3"
end
View
@@ -398,5 +398,21 @@ def unknown(*args)
klass.start(["unknown", "--bar", "baz"]).should == []
klass.start(["unknown", "foo", "--bar", "baz"]).should == ["foo"]
end
+
+ it "strict args works in the inheritance chain" do
+ parent = Class.new(Thor) do
+ strict_args_position!
+ end
+
+ klass = Class.new(parent) do
+ desc "unknown", "passing unknown options"
+ def unknown(*args)
+ args
+ end
+ end
+
+ klass.start(["unknown", "--bar", "baz"]).should == []
+ klass.start(["unknown", "foo", "--bar", "baz"]).should == ["foo"]
+ end
end
end

0 comments on commit 58ef2cb

Please sign in to comment.