Ruby_types

I’ve been thinking about type-checking ruby for some time. I know there are gems like sorbet, steep, dry-types. My little home-cooked solution is 35 lines of code. Works like this:

require './mw_vars.rb'

X = Globals::Vars.new

X.rules(:price) {
  rule proc { Integer === it }
  rule proc { it > 0 }
}

X.rules(:range) {
  rule proc { Range === it }
  rule proc { it.first == 1 && it.last <= 100 }
}

X.rules(:label) {
  rule proc { String === it }
}

def data(price, range, label)
  X.price, X.range, X.label = price, range, label
  p 'ok'
end

data 12, (1..50), 'hejsan' # => ok
data 300, (2..22), :hej # => Line: 35, Error value for [range = 2..22 (Range)]