egonSchiele / contracts.ruby

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

Inheritance class << self problem #208

Closed pavel-manylov closed 8 years ago

pavel-manylov commented 9 years ago

Bare-minimum code to reproduce the problem:

require 'contracts'

class A
  include Contracts::Core
  include Contracts::Builtin
end

class B < A
  Contract Num => Num
  def self.plus_two(num)
    num + 2
  end
end

class C < A

  class << self
    Contract Num => Num
    def plus_two(num)
      num + 2
    end
  end

end

classes B and C should be identical, however, Contracts raises an error for class C:

/Users/rk/.rvm/gems/ruby-2.2.3@soft24/gems/contracts-0.12.0/lib/contracts/engine/eigenclass.rb:38:in `validate!': In order to use contracts in singleton class, please include Contracts module in original class (Contracts::ContractsNotIncluded)
    Example:

    ```ruby
    class Example
      include Contracts  # this line is required
      class << self
        # you can use `Contract` definition here now
      end
    end
from /Users/rk/.rvm/gems/ruby-2.2.3@soft24/gems/contracts-0.12.0/lib/contracts/engine/base.rb:42:in `decorate'
from /Users/rk/.rvm/gems/ruby-2.2.3@soft24/gems/contracts-0.12.0/lib/contracts/decorators.rb:33:in `Contract'
from contracts_test.rb:18:in `singleton class'
from contracts_test.rb:17:in `<class:C>'
from contracts_test.rb:15:in `<main>'
egonSchiele commented 9 years ago

Interesting. I can reproduce this. It happens even if I add an include Contracts in class C.

waterlink commented 8 years ago

Fixed on master since #211 landed