Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does Ruby support type-hinting?

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.

like image 993
NONE Avatar asked Jul 16 '13 19:07

NONE


People also ask

Does Ruby have type inference?

Ruby Type Inference for IDEA is a complete re-think of how JetBrains plans to approach type inference for Ruby in their IDEA / RubyMine IDE. This does use type inference, but it uses dynamic type inference, not static.

Does Ruby have a type system?

Ruby is a dynamically typed language, which means the interpreter tries to infer the data type of variables and object properties at runtime. This generally leads to programs being more dynamic and easier (faster) to code, and the interpreter/compiler loading code faster.

What is type hinted?

Type hinting is a formal solution to statically indicate the type of a value within your Python code. It was specified in PEP 484 and introduced in Python 3.5. Here's an example of adding type information to a function.


1 Answers

Ruby 3 will introduce types to Ruby (Source). You can already use Sorbet now to add types to your ruby code.

like image 127
jayeff Avatar answered Oct 15 '22 22:10

jayeff