All Projects → ruby → Rbs

ruby / Rbs

Licence: other
Type Signature for Ruby

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Rbs

pyrser
A PEG Parsing Tool
Stars: ✭ 32 (-97%)
Mutual labels:  type-checking
progge.rs
Program analysis playground for a simple, imperative language
Stars: ✭ 29 (-97.28%)
Mutual labels:  type-checking
Ow
Function argument validation for humans
Stars: ✭ 3,415 (+220.06%)
Mutual labels:  type-checking
micropython-stubs
Stubs of common micropython modules to aid in code completion, static typechecking and overall development
Stars: ✭ 46 (-95.69%)
Mutual labels:  type-checking
Typology
Swift type checking and semantic analysis for developer tools
Stars: ✭ 68 (-93.63%)
Mutual labels:  type-checking
typical
Isomorphic, functional type-checking for Javascript
Stars: ✭ 17 (-98.41%)
Mutual labels:  type-checking
IntelliJ-Luanalysis
Type-safe Lua IDE Plugin for IntelliJ IDEA
Stars: ✭ 118 (-88.94%)
Mutual labels:  type-checking
Tl
The compiler for Teal, a typed dialect of Lua
Stars: ✭ 716 (-32.9%)
Mutual labels:  type-checking
infrared
✨🚀 Blazing fast, inferred static type checker for JavaScript.
Stars: ✭ 46 (-95.69%)
Mutual labels:  type-checking
Hammox
🏝 automated contract testing via type checking for Elixir functions and mocks
Stars: ✭ 289 (-72.91%)
Mutual labels:  type-checking
strongtyping
Decorator which checks whether the function is called with the correct type of parameters.
Stars: ✭ 85 (-92.03%)
Mutual labels:  type-checking
floweret
Runtime type annotations for CoffeeScript (and JavaScript too!)
Stars: ✭ 20 (-98.13%)
Mutual labels:  type-checking
Mypy Pycharm Plugin
A simple plugin that allows running mypy from PyCharm and navigate between errors
Stars: ✭ 274 (-74.32%)
Mutual labels:  type-checking
typist-json
A simple runtime JSON type checker.
Stars: ✭ 25 (-97.66%)
Mutual labels:  type-checking
Objectmodel
Strong Dynamically Typed Object Modeling for JavaScript
Stars: ✭ 415 (-61.11%)
Mutual labels:  type-checking
typeforce
Another biased type checking solution for Javascript
Stars: ✭ 22 (-97.94%)
Mutual labels:  type-checking
mps-coderules
Type checking and logical inference for JetBrains MPS
Stars: ✭ 24 (-97.75%)
Mutual labels:  type-checking
Bazel Mypy Integration
🐍🌿💚 Integrate MyPy type-checking into your Python Bazel builds
Stars: ✭ 40 (-96.25%)
Mutual labels:  type-checking
Caer
High-performance Vision library in Python. Scale your research, not boilerplate.
Stars: ✭ 452 (-57.64%)
Mutual labels:  type-checking
Write You A Haskell
Building a modern functional compiler from first principles. (http://dev.stephendiehl.com/fun/)
Stars: ✭ 3,064 (+187.16%)
Mutual labels:  type-checking

RBS

RBS is a language to describe the structure of Ruby programs. You can write down the definition of a class or module: methods defined in the class, instance variables and their types, and inheritance/mix-in relations. It also allows declaring constants and global variables.

The following is a small example of RBS for a chat app.

module ChatApp
  VERSION: String

  class User
    attr_reader login: String
    attr_reader email: String

    def initialize: (login: String, email: String) -> void
  end

  class Bot
    attr_reader name: String
    attr_reader email: String
    attr_reader owner: User

    def initialize: (name: String, owner: User) -> void
  end

  class Message
    attr_reader id: String
    attr_reader string: String
    attr_reader from: User | Bot                     # `|` means union types: `#from` can be `User` or `Bot`
    attr_reader reply_to: Message?                   # `?` means optional type: `#reply_to` can be `nil`

    def initialize: (from: User | Bot, string: String) -> void

    def reply: (from: User | Bot, string: String) -> Message
  end

  class Channel
    attr_reader name: String
    attr_reader messages: Array[Message]
    attr_reader users: Array[User]
    attr_reader bots: Array[Bot]

    def initialize: (name: String) -> void

    def each_member: () { (User | Bot) -> void } -> void  # `{` and `}` means block.
                   | () -> Enumerator[User | Bot, void]   # Method can be overloaded.
  end
end

Installation

Install the rbs gem. $ gem install rbs from the command line, or add a line in your Gemfile.

gem "rbs"

CLI

The gem ships with the rbs command line tool to demonstrate what it can do and help develop RBS.

$ rbs version
$ rbs list
$ rbs ancestors ::Object
$ rbs methods ::Object
$ rbs method Object then

Library

There are two important concepts, environment and definition.

An environment is a dictionary that keeps track of all declarations. What is the declaration associated with String class? An environment will give you the answer.

A definition gives you the detail of the class. What is the type of the return value of gsub method of the String class? The definition for String class knows the list of methods it provides and their types.

The following is a small code to retrieve the definition of the String#gsub method.

require "rbs"

loader = RBS::EnvironmentLoader.new()

# loader.add(path: Pathname("sig"))   # Load .rbs files from `sig` directory
# loader.add(library: "pathname")     # Load pathname library

environment = RBS::Environment.from_loader(loader).resolve_type_names

# ::String
string = RBS::TypeName.new(name: :String, namespace: RBS::Namespace.root)

# Class declaration for ::String
decl = environment.class_decls[string]

# Builder provides the translation from `declaration` to `definition`
builder = RBS::DefinitionBuilder.new(env: environment)

# Definition of instance of String
instance = builder.build_instance(string)

# Print the types of `gsub` method:
puts instance.methods[:gsub].method_types.join("\n")
# Outputs =>
#  (::Regexp | ::string pattern, ::string replacement) -> ::String
#  (::Regexp | ::string pattern, ::Hash[::String, ::String] hash) -> ::String
#  (::Regexp | ::string pattern) { (::String match) -> ::_ToS } -> ::String
#  (::Regexp | ::string pattern) -> ::Enumerator[::String, self]

# Definition of singleton of String
singleton = builder.build_singleton(string)
# No `gsub` method for String singleton
puts singleton.methods[:gsub]

Guides

Development

After checking out the repo, run bin/setup to install dependencies. Then, run bundle exec rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.

Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/rbs.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].