How Can contracts.ruby Be Used in the Community With Duck Typing Culture?

contracts.ruby, design-by-contract, ruby

So, given simple example:

1
2
3
4
Contract Num, Num => Num
def add(a, b)
  a + b
end

One can ask: “But it is ruby, what about duck typing, I want just pass two things that have certain methods defined on them”

And my answer, you can easily do that:

1
2
3
4
5
Contract RespondTo[:save, :has_valid?], RespondTo[:to_s] => Any
def assign_user_a_default_email(user, default_email)
  user.email = default_email unless user.has_valid?(:email)
  user.save
end

This is a built-in RespondTo contract. You can get a list of all of them here: http://egonschiele.github.io/contracts.ruby/#built-in-contracts

If you have any questions, you can always ping me at twitter @waterlink000