5 ms·
Thanks everyone, reading through your answers and thinking through the bugs I've solved has lead me to some interesting conclusions. (The language I'm using is
by DanWaterworth 15y ago
Thanks everyone, reading through your answers and thinking through the bugs I've solved has lead me to some interesting conclusions.
(The language I'm using is Ruby).
The problem is not that Ruby isn't statically typed. It's that what I think of as type errors in Ruby pass without incident. The most irritating example of this is that if you don't pass enough arguments to a proc object the other parameters get filled in with nil. In my mind this should throw a type error, but in Ruby it doesn't.
Perhaps this is why Rubyists are so keen on testing, because their language is full of implicit behaviours.
- ajuc 15y ago> The most irritating example of this is that if you don't pass enough arguments to a proc object the other parameters get filled in with nil. In my mind this should throw a type error, but in Ruby it doesn't. Maybe automate checking this? I don't know Ruby much, but there's .arity method on proc that seems to be apropriate for this.
- haven 15y agoProc doesn't check arity. That is just how proc and Proc.new are expected to work. If you want to check the arity, use lambda or ->. x = ->(a, b) { puts a, b } x.call #ArgumentError: wrong number of arguments (0 for 2) x = lambda { |a, b| puts a, b } x.("just one argument") #ArgumentError: wrong number of arguments (1 for 2)
- DanWaterworth 15y agoSorry, it wasn't a proc object, it was a block. The following is a potential fix, but it's irritating to have to write it. blk = lambda { |arg1, arg2| # do something } obj.some_method(&blk)