60
loading...
This website collects cookies to deliver better user experience
class MyClass
def my_method : (my_param: String) -> String
end
rbs
gem directly into your current project:gem install rbs
int number = 0;
number = "Hi, number!";
error: incompatible types: String cannot be converted to int
number = 0;
number = "Hi, number!";
puts number // Successfully prints "Hi, number!"
number = 2;
sum = "2" + 2;
puts sum
Integer
and a String
). Ruby will throw the following error:main.rb:2:in `+': no implicit conversion of Integer into String (TypeError)
from main.rb:2:in `<main>'
> number = 2
sum = "2" + 2
console.log(sum)
> 22 // it concatenates both values
class Badger
def initialize(brand)
@brand = brand
end
def brand?
@brand
end
end
class Honey < Badger
def initialize(brand: "Honeybadger", sweet: true)
super(brand)
@sweet = sweet
end
def sweet?
@sweet
end
end
class Brand
attr_reader brand : String
def initialize : (brand: String) -> void
end
class Honey < Brand
@sweet : bool
def initialize : (brand: String, ?sweet: bool) -> void
def sweet? : () -> bool
end
initialize
method of the Honey
class, for example, receives one String
and one boolean
parameter and returns nothing.rbs
to scaffold types for existing classes.rbs help
in the console and check the result:prototype
since it analyzes ASTs of the source code file provided as a param to generate "approximate" RBS code.class Animal
def initialize(weight)
@weight = weight
end
def breathe
puts "Inhale/Exhale"
end
end
class Mammal < Animal
def initialize(weight, is_terrestrial)
super(weight)
@is_terrestrial = is_terrestrial
end
def nurse
puts "I'm breastfeeding"
end
end
class Cat < Mammal
def initialize(weight, n_of_lives, is_terrestrial: true)
super(weight, is_terrestrial)
@n_of_lives = n_of_lives
end
def speak
puts "Meow"
end
end
rbs prototype rb animal.rb mammal.rb cat.rb
class Animal
def initialize: (untyped weight) -> untyped
def breathe: () -> untyped
end
class Mammal < Animal
def initialize: (untyped weight, untyped is_terrestrial) -> untyped
def nurse: () -> untyped
end
class Cat < Mammal
def initialize: (untyped weight, untyped n_of_lives, ?is_terrestrial: bool is_terrestrial) -> untyped
def speak: () -> untyped
end
untyped
references to the real ones. Some discussions aiming to find better ways to accomplish this are happening right now in the community .rbs
tool won't be of much help due to its dynamic nature.class Meta
define_method :greeting, -> { puts 'Hi there!' }
end
Meta.new.greeting
class Meta
end
Cat
will inherit to:class Terrestrial < Animal
def initialize(weight)
super(weight)
end
def run
puts "Running..."
end
end
interface _CanRun
# Requires `<<` operator which accepts `Terrestrial` object.
def <<: (Terrestrial) -> void
end
def run: (_CanRun) -> void
def fly: () -> (Mammal | Bird | Insect)
def evolutionary_cousins: () -> Enumerator[Animal, void] | { (Animal) -> void } -> void
gem install typeprof
Animal
class:class Animal
def initialize(weight)
@weight = weight
end
def die(age)
if age > 50
true
elsif age <= 50
false
elsif age < 0
nil
end
end
end
Animal.new(100).die(65)
age
and the further call to the same method, TypeProf can smartly infer the types manipulated in the code.typeprof animal.rb
command, that should be the output:## Classes
class Animal
@weight: Integer
def initialize: (Integer weight) -> Integer
def die: (Integer age) -> bool?
end