egonSchiele / contracts.ruby

Contracts for Ruby.
http://egonschiele.github.com/contracts.ruby
BSD 2-Clause "Simplified" License
1.44k stars 82 forks source link

KeywordArgs causes error when used with Func #209

Closed pavel-manylov closed 8 years ago

pavel-manylov commented 8 years ago

Code to replicate problem:

require 'contracts'
include Contracts::Core
include Contracts::Builtin

Contract KeywordArgs[name: Optional[String]], Func[String => String] => String
def hey_you(name: 'John', &block)
  "Hey, #{yield name}!"
end

puts hey_you{|name| name.reverse }
puts hey_you(name: 'George'){|name| name.reverse }

Raised error:

/Users/rk/.rvm/gems/ruby-2.2.3@soft24/gems/contracts-0.12.0/lib/contracts/builtin_contracts.rb:390:in `valid?': undefined method `keys' for #<Proc:0x007fbf8a16ba60@keyword_args_vs_func.rb:10> (NoMethodError)
    from /Users/rk/.rvm/gems/ruby-2.2.3@soft24/gems/contracts-0.12.0/lib/contracts/validators.rb:53:in `block (2 levels) in <module:Validators>'
    from /Users/rk/.rvm/gems/ruby-2.2.3@soft24/gems/contracts-0.12.0/lib/contracts/call_with.rb:18:in `[]'
    from /Users/rk/.rvm/gems/ruby-2.2.3@soft24/gems/contracts-0.12.0/lib/contracts/call_with.rb:18:in `block in call_with'
    from /Users/rk/.rvm/gems/ruby-2.2.3@soft24/gems/contracts-0.12.0/lib/contracts/call_with.rb:13:in `times'
    from /Users/rk/.rvm/gems/ruby-2.2.3@soft24/gems/contracts-0.12.0/lib/contracts/call_with.rb:13:in `call_with'
    from /Users/rk/.rvm/gems/ruby-2.2.3@soft24/gems/contracts-0.12.0/lib/contracts/method_handler.rb:138:in `block in redefine_method'
    from keyword_args_vs_func.rb:10:in `<main>'
egonSchiele commented 8 years ago

I can reproduce. puts hey_you{|name| name.reverse } raises the error. Optional args + blocks seem to lead to many issues.

egonSchiele commented 8 years ago

PR is here https://github.com/egonSchiele/contracts.ruby/pull/230

egonSchiele commented 8 years ago

Fixed