Does Ruby support type-hinting?

ruby, type-hinting

Solution

Ruby does not have such thing, but all you have to do is add a single line as so:

def do_something(x)
  raise "Argument error blah blah" unless x.kind_of?(MyClass)
  ...
end

Not a big deal. But if you feel it is too verbose, then just define a method:

module Kernel
  def verify klass, arg
    raise "Argument error blah blah" unless arg.kind_of?(klass)
  end
end

and put that in the first line:

def do_something(x)
  verify(MyClass, x)
  ...
end

Problem

PHP Example: ``` function do_something(int $i) { return $i + 2; } ``` Ruby Example: ``` class MyClass # ... end def do_something(MyClass x) x.prop1 = "String..." end ``` Is there anything similar to this? Thanks.

Original source