### Comprehensive RBS Test Command Line Example Source: https://github.com/ruby/rbs/blob/master/docs/sigs.md This example shows a complete command line for executing RBS tests, combining multiple environment variables. It sets the log level to error, targets specific classes (`Kaigi::*`), skips a particular class (`Kaigi::MonkeyPatch`), configures signature loading options, enables exception raising on type errors, and includes Ruby options for bundler and RBS test setup. ```console $ RBS_TEST_LOGLEVEL=error \ RBS_TEST_TARGET='Kaigi::*' \ RBS_TEST_SKIP='Kaigi::MonkeyPatch' \ RBS_TEST_OPT='-rset -rpathname -Isig -Iprivate' \ RBS_TEST_RAISE=true \ RUBYOPT='-rbundler/setup -rrbs/test/setup' \ bundle exec rake test ``` -------------------------------- ### Load RBS Test Setup with Bundler Source: https://github.com/ruby/rbs/blob/master/docs/sigs.md Shows how to properly load the `rbs/test/setup` library when using Bundler. It requires explicitly setting `RUBYOPT` to include `bundler/setup` alongside `rbs/test/setup`. ```console $ RUBYOPT='-rbundler/setup -rrbs/test/setup' bundle exec rake test ``` -------------------------------- ### Load RBS Test Setup via Command Line Source: https://github.com/ruby/rbs/blob/master/docs/sigs.md Instructions on how to load the `rbs/test/setup` library for signature testing. This can be done using the `-r` option with the `ruby` command or by setting the `RUBYOPT` environment variable. ```console $ ruby -r rbs/test/setup run_tests.rb $ RUBYOPT='-rrbs/test/setup' rake test ``` -------------------------------- ### RBS Proc Type Definition Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Shows examples of defining procedure (Proc) types in RBS. These examples illustrate how to specify parameter types (including optional and keyword parameters) and the return type for a procedure. ```rbs ^(Integer) -> String # A procedure with an `Integer` parameter and returns `String` ^(?String, size: Integer) -> bool # A procedure with `String` optional parameter, `size` keyword of `Integer`, and returns `bool` ``` -------------------------------- ### Example of Nested RBS Module and Class Declarations Source: https://github.com/ruby/rbs/blob/master/docs/architecture.md Illustrates how nested modules and classes are declared in RBS, showing both the inline nesting syntax and the fully qualified name declaration. This example is used to explain how RBS::Environment organizes and indexes definitions. ```rbs module Hello class World end end class Hello::World end ``` -------------------------------- ### RBS Class Singleton Type Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Provides examples of class singleton type definitions in RBS, illustrating how to denote the type of a singleton object for a class. ```rbs singleton(String) singleton(::Hash) # Class singleton type cannot be parametrized. ``` -------------------------------- ### RBS Tuple Type Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Shows examples of tuple types in RBS, representing array objects with a fixed size and heterogeneous elements. ```rbs [ ] # Empty like `[]` [String] # Single string like `["hi"]` [Integer, Integer] # Pair of integers like `[1, 2]` [Symbol, Integer, Integer] # Tuple of Symbol, Integer, and Integer like `[:pair, 30, 22]` ``` -------------------------------- ### RBS Alias Type Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Shows examples of alias type definitions in RBS, covering simple aliases, namespaced aliases, and generic type aliases. ```rbs name ::JSON::t # Alias name with namespace list[Integer] # Type alias can be generic ``` -------------------------------- ### Install Third-Party Gem RBS Definitions Source: https://github.com/ruby/rbs/blob/master/docs/collection.md Shows the `rbs collection install` command, which copies RBS definitions from configured sources (e.g., `gem_rbs_collection`) into the local `.gem_rbs_collection/` directory, making them available for `rbs` commands. ```console $ rbs collection install Installing ast:2.4 (ruby/gem_rbs_collection@4b1a2a2f64c) ... It's done! 42 gems's RBSs now installed. ``` -------------------------------- ### RBS Use Directive Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Provides practical examples of `use` directives in RBS, demonstrating how to define direct type aliases, create aliased type names, and import entire namespaces for simplified type referencing. ```rbs use RBS::Namespace # => Defines `Namespace` use RBS::TypeName as TN # => Defines `TN` use RBS::AST::* # => Defines modules under `::RBS::AST::` namespace ``` -------------------------------- ### Define Zero-Argument Methods in RBS Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Demonstrates how to define an RBS signature for a Ruby method that takes no arguments and returns a boolean value, using `String#empty?` as an example. ```Ruby "".empty? # => true "hello".empty? # => false ``` ```RBS class String def empty?: () -> bool end ``` -------------------------------- ### Configure RBS Collection Sources and Gems Source: https://github.com/ruby/rbs/blob/master/docs/collection.md Provides detailed examples for configuring `rbs_collection.yaml`. This includes defining multiple download sources (remote Git repositories or local paths) and managing individual gem RBS installations by explicitly including or ignoring specific gems. ```yaml # rbs_collection.yaml # Download sources. # You can add own collection git repository. sources: - name: ruby/gem_rbs_collection remote: https://github.com/ruby/gem_rbs_collection.git revision: main repo_dir: gems # You can also add a local path as a collection source optionally. - type: local path: path/to/local/dir # A directory to install the downloaded RBSs path: .gem_rbs_collection gems: # If the Gemfile.lock doesn't contain csv gem but you use csv gem, # you can write the gem name explicitly to install RBS of the gem. - name: csv # If the Gemfile.lock contains nokogiri gem but you don't want to use the RBS, # you can ignore the gem. # `rbs collection` avoids to install nokogiri gem's RBS by this change. # It is useful if the nokogiri RBS has a problem, such as compatibility issue with other RBS. - name: nokogiri ignore: true ``` -------------------------------- ### Install RBS 3.0 via Gemfile Source: https://github.com/ruby/rbs/wiki/Release-Note-3.0 Specifies the RBS gem dependency in a Ruby project's Gemfile, ensuring version 3.0.0 or compatible is used for installation. ```Ruby gem 'rbs', '~> 3.0.0' ``` -------------------------------- ### RBS Class Instance Type Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Illustrates how to define class instance types in RBS, showing examples for basic class instances, namespaced classes, and generic class instances with type applications. ```rbs Integer # Instance of Integer class ::Integer # Instance of ::Integer class Hash[Symbol, String] # Instance of Hash class with type application of Symbol and String ``` -------------------------------- ### RBS Interface Type Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Demonstrates the syntax for defining interface types in RBS, including simple interface names and interfaces with namespaces and type applications. ```rbs _ToS # _ToS interface ::Enumerator::_Each[String] # Interface name with namespace and type application ``` -------------------------------- ### Install RBS Gem Source: https://github.com/ruby/rbs/blob/master/README.md Provides instructions and the Ruby code snippet for installing the `rbs` gem using `gem install` from the command line or by adding it to a `Gemfile`. ```ruby gem "rbs" ``` -------------------------------- ### RBS Record Type Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Illustrates the syntax for record types in RBS, representing hash objects with a fixed set of keys and heterogeneous values. ```rbs { id: Integer, name: String } # Hash object like `{ id: 31, name: String }` ``` -------------------------------- ### RBS UnexpectedBlockError Example Source: https://github.com/ruby/rbs/blob/master/docs/sigs.md An example of an `UnexpectedBlockError` message, reported when a block is provided to a method that does not expect one. The error message includes the method's signature. ```text ERROR -- : [Kaigi::Conference#speakers] UnexpectedBlockError: unexpected block is given for `() -> ::Array[::Kaigi::Speaker]` ``` -------------------------------- ### Install RBS Gem and Specify Dependency Source: https://github.com/ruby/rbs/wiki/Release-Note-3.7 Instructions for installing the RBS gem via the command line and adding it to your Gemfile for project dependency management, ensuring the correct version is used. ```bash $ gem install rbs ``` ```ruby gem 'rbs', '~> 3.7.0' ``` -------------------------------- ### Install RBS Project Dependencies Source: https://github.com/ruby/rbs/blob/master/README.md This command sets up the necessary dependencies for the RBS project after cloning the repository, preparing the environment for development. ```bash bin/setup ``` -------------------------------- ### RBS Type Variable Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Provides examples of type variables in RBS, demonstrating their usage and scope within class, module, interface, alias, or generic method declarations. ```rbs U T S Elem ``` ```rbs class Ref[T] # Object is scoped in the class declaration. @value: T # Type variable `T` def map: [X] { (T) -> X } -> Ref[X] # X is a type variable scoped in the method type. end ``` -------------------------------- ### Initialize RBS Collection Configuration Source: https://github.com/ruby/rbs/blob/master/docs/collection.md Demonstrates how to generate the initial `rbs_collection.yaml` file using `rbs collection init` and displays its default content, which defines sources (like `ruby/gem_rbs_collection`) and the default installation path (`.gem_rbs_collection`). ```console $ rbs collection init created: rbs_collection.yaml ``` ```yaml # Download sources sources: - name: ruby/gem_rbs_collection remote: https://github.com/ruby/gem_rbs_collection.git revision: main repo_dir: gems # A directory to install the downloaded RBSs path: .gem_rbs_collection # gems: # # If you want to avoid installing rbs files for gems, you can specify them here. # - name: GEM_NAME # ignore: true ``` -------------------------------- ### RBS Method Type Parameter Usage Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md These examples demonstrate various parameter configurations in RBS method types, including positional (required and optional), rest parameters, and keyword parameters (required, optional, and with variable names). They illustrate how to define different argument patterns for methods. ```rbs # Two required positional `Integer` parameters, and returns `String` (Integer, Integer) -> String # Two optional parameters `size` and `name`. # `name` is a optional parameter with optional type so that developer can omit, pass a string, or pass `nil`. (?Integer size, ?String? name) -> String # Method type with a rest parameter (*Integer, Integer) -> void # `size` is a required keyword, with variable name of `sz`. # `name` is a optional keyword. # `created_at` is a optional keyword, and the value can be `nil`. (size: Integer sz, ?name: String, ?created_at: Time?) -> void ``` -------------------------------- ### RBS Intersection Type Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Shows examples of intersection types in RBS, where a type must satisfy all of the given types simultaneously. Also notes operator precedence. ```rbs _Reader & _Writer # _Reader and _Writer ``` -------------------------------- ### RBS Interface Declaration Example Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Provides an example of an interface declaration in RBS, defining methods that a class implementing this interface must provide. It specifies method signatures for `hash` and `eql?`. ```rbs interface _Hashing def hash: () -> Integer def eql?: (untyped) -> bool end ``` -------------------------------- ### Install RBS 3.9 Gem Source: https://github.com/ruby/rbs/wiki/Release-Note-3.9 This snippet provides the RubyGems command to install the specific version 3.9.0 of the RBS gem. It's typically used in a Gemfile for Bundler or directly via the command line for project setup. ```Ruby gem 'rbs', '~> 3.9.0' ``` -------------------------------- ### Install clang-format on Ubuntu/Debian Source: https://github.com/ruby/rbs/blob/master/README.md This command installs the `clang-format` tool on Ubuntu or Debian systems using `apt-get`, essential for maintaining C code style guidelines. ```bash sudo apt-get install clang-format ``` -------------------------------- ### RBS Optional Type Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Demonstrates the syntax for optional types in RBS, indicating that a value can be of a specified type or `nil`. ```rbs Integer? Array[Integer?] ``` -------------------------------- ### Install clang-format on Windows Source: https://github.com/ruby/rbs/blob/master/README.md This command installs the LLVM suite, which includes `clang-format`, on Windows using Chocolatey, enabling C code formatting on the platform. ```bash choco install llvm ``` -------------------------------- ### RBS ArgumentError Example Source: https://github.com/ruby/rbs/blob/master/docs/sigs.md An example of an `ArgumentError` message, which occurs when a method receives an unexpected argument or is missing a required one. The error message includes the expected method type signature. ```text [Kaigi::Speaker.new] ArgumentError: expected method type (size: ::Symbol, email: ::String, name: ::String) -> ::Kaigi::Speaker ``` -------------------------------- ### Define Methods with Keyword Arguments in RBS Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Demonstrates how to define RBS signatures for methods that accept optional keyword arguments, using `String#lines` which can take an optional String and an optional boolean `chomp` keyword argument. ```Ruby "hello\nworld\n".lines # => ["hello\n", "world\n"] "hello world".lines(' ') # => ["hello ", " ", "world"] "hello\nworld\n".lines(chomp: true) # => ["hello", "world"] ``` ```RBS class String def lines: (?String, ?chomp: bool) -> Array[String] end ``` -------------------------------- ### RBS ArgumentTypeError Example Source: https://github.com/ruby/rbs/blob/master/docs/sigs.md An example of an `ArgumentTypeError` message, indicating that an argument received an unexpected type. The error details the expected type (`::String`) versus the actual given value (`:"matsumoto@soutaro.com"`). ```text ERROR -- : [Kaigi::Speaker.new] ArgumentTypeError: expected `::String` (email) but given `:\"matsumoto@soutaro.com\"` ``` -------------------------------- ### Install RBS Gem Locally Source: https://github.com/ruby/rbs/blob/master/README.md This command installs the RBS gem onto the local machine, making it available for local testing or integration with other projects without publishing. ```bash bundle exec rake install ``` -------------------------------- ### Define and Use Hash#keys with Type Variables (Ruby/RBS) Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Demonstrates the usage of the `Hash#keys` method in Ruby and its corresponding type signature in RBS. It illustrates how type variables `K` and `V` are employed to define generic `Hash` types, and how the `keys` method returns an `Array` parameterized by the key type `K`. ```ruby h = { "a" => 100, "b" => 200, "c" => 300, "d" => 400 } h.keys # => ["a", "b", "c", "d"] ``` ```rbs class Hash[K, V] def keys: () -> Array[K] end ``` -------------------------------- ### Define Variable-Argument Methods in RBS Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Shows how to define an RBS signature for a Ruby method that accepts a variable number of arguments (splat arguments), exemplified by `String#end_with?` which takes multiple String arguments. ```Ruby "hello?".end_with?("!") # => false "hello?".end_with?("?") # => true "hello?".end_with?("", "!") # => true "hello?".end_with?(".", "!") # => false ``` ```RBS class String def end_with?: (*String) -> bool end ``` -------------------------------- ### Install clang-format on macOS Source: https://github.com/ruby/rbs/blob/master/README.md This command installs the `clang-format` tool on macOS using Homebrew, which is required for enforcing consistent C code formatting within the project. ```bash brew install clang-format ``` -------------------------------- ### Prevent RBS Installation via Gemfile Source: https://github.com/ruby/rbs/blob/master/docs/collection.md Shows how to use `require: false` in a `Gemfile` entry to prevent `rbs collection` from installing RBS definitions for a specific gem. This is the recommended way to avoid unnecessary RBS installations. ```ruby # Gemfile gem 'GEM_NAME', require: false ``` -------------------------------- ### Define Methods with Nilable Return Types in RBS Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Explains how to define RBS signatures for methods that can return `nil` using the `?` suffix, and also shows multiple signatures for `Enumerable#first` based on argument presence. ```Ruby [1, 2, 3].first # => 1 [].first # => nil [1, 2, 3].first(2) # => [1, 2] [].first(2) # => [] ``` ```RBS class Enumerable[Elem] def first: () -> Elem? | (Integer) -> Array[Elem] end ``` -------------------------------- ### Start RBS Interactive Console Source: https://github.com/ruby/rbs/blob/master/README.md This command launches an interactive Ruby console, allowing developers to experiment with the RBS project's code and features in real-time. ```bash bin/console ``` -------------------------------- ### RBS::Parser Entrypoints for Type Parsing Source: https://github.com/ruby/rbs/blob/master/docs/architecture.md Details the three primary methods provided by RBS::Parser for parsing different components of RBS syntax: method types, general types, and entire RBS files. These methods are implemented in a C extension. ```APIDOC RBS::Parser: parse_method_type(type_string: String) -> MethodType Parses a method type string, e.g., `[T] (String) { (IO) -> T } -> Array[T]`. parse_type(type_string: String) -> Type Parses a general type string, e.g., `Hash[Symbol, untyped]`. parse_signature(signature_string: String) -> Signature Parses an entire RBS file signature. ``` -------------------------------- ### Define Single-Argument Methods in RBS Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Illustrates how to define an RBS signature for a Ruby method that accepts a single argument, using `String#include?` which takes a String and returns a boolean. ```Ruby "homeowner".include?("house") # => false "homeowner".include?("meow") # => true ``` ```RBS class String def include?: (String) -> bool end ``` -------------------------------- ### Define Methods with Block Arguments in RBS Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Explains how to define RBS signatures for methods that accept a block, including cases where the method can also return an Enumerator if no block is given, using `Array#filter`. ```Ruby [1,2,3,4,5].filter {|num| num.even? } # => [2, 4] %w[ a b c d e f ].filter {|v| v =~ /[aeiou]/ } # => ["a", "e"] [1,2,3,4,5].filter ``` ```RBS class Array[Elem] def filter: () { (Elem) -> boolish } -> ::Array[Elem] | () -> ::Enumerator[Elem, ::Array[Elem]] end ``` -------------------------------- ### Define Methods with Multiple Signatures in RBS Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Demonstrates how to define multiple RBS signatures for a single Ruby method that behaves differently based on argument types, using `Array#*` which can take either a String or an Integer. ```Ruby [1, 2, 3] * "," # => "1,2,3" [1, 2, 3] * 2 # => [1, 2, 3, 1, 2, 3] ``` ```RBS class Array[Elem] def *: (String) -> String | (Integer) -> Array[Elem] end ``` -------------------------------- ### Install RBS Gem via Bundler Source: https://github.com/ruby/rbs/wiki/Release-Note-3.1 This snippet shows how to include the RBS gem in your project's Gemfile for installation via Bundler, specifying a compatible version range. ```ruby gem 'rbs', '~> 3.1.0' ``` -------------------------------- ### Define Methods with Union Types in RBS Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Illustrates how to use union types in RBS signatures for methods that accept arguments of multiple possible types, exemplified by `String#<<` which takes either a String or an Integer. ```Ruby a = "hello " a << "world" #=> "hello world" a << 33 #=> "hello world!" ``` ```RBS class String def <<: (String | Integer) -> String end ``` -------------------------------- ### Useful RBS Command-Line Tools for Signature Contribution Source: https://github.com/ruby/rbs/blob/master/docs/CONTRIBUTING.md This section lists various command-line tools essential for generating, annotating, querying, sorting, validating, and testing RBS type definitions. These tools streamline the process of creating and maintaining accurate type signatures for Ruby's Core API and Standard Libraries. ```Shell rbs prototype runtime --merge String ``` ```Shell rbs prototype runtime --merge --method-owner=Numeric Integer ``` ```Shell rbs annotate core/string.rbs ``` ```Shell bin/query-rdoc String#initialize ``` ```Shell bin/sort core/string.rbs ``` ```Shell rbs -r LIB validate ``` ```Shell rake generate:stdlib_test[String] ``` ```Shell rake test/stdlib/Array_test.rb ``` -------------------------------- ### RBS Union Type Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Illustrates the syntax for union types in RBS, showing how to define a type that can be one of several specified types, including nested union types within generics. ```rbs Integer | String # Integer or String Array[Integer | String] # Array of Integer or String ``` -------------------------------- ### Execute RBS Signature Tests Source: https://github.com/ruby/rbs/blob/master/docs/sigs.md This console command runs signature tests for specified modules. It leverages `RBS_TEST_TARGET` to define the scope of testing and uses `bundle exec ruby` to execute the test script with the `rbs/test/setup` library loaded. ```console $ RBS_TEST_TARGET='Foo::*' bundle exec ruby -r rbs/test/setup test/foo_test.rb ``` -------------------------------- ### Install RBS Gem with Bundler Source: https://github.com/ruby/rbs/wiki/Release-Note-3.6 Shows how to add the RBS gem version 3.6.0 to a Ruby project's Gemfile for Bundler installation, ensuring the correct version is used. ```ruby gem 'rbs', '~> 3.6.0' ``` -------------------------------- ### Partition Enumerable into Tuples (Ruby/RBS) Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Demonstrates the `Enumerable#partition` method in Ruby, which divides elements into two arrays based on a given condition. The RBS type signature highlights its return type as a 2-item tuple of `Array`s, illustrating how tuples are used to represent fixed-size collections with potentially mixed types. ```ruby (1..6).partition { |v| v.even? } # => [[2, 4, 6], [1, 3, 5]] ``` ```rbs class Enumerable[Elem] def partition: () { (Elem) -> boolish } -> [Array[Elem], Array[Elem]] | () -> ::Enumerator[Elem, [Array[Elem], Array[Elem] ]] end ``` -------------------------------- ### Define Methods with Optional Positional Arguments in RBS Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Explains how to define an RBS signature for a Ruby method with optional positional arguments, using `String#ljust` which takes an Integer and an optional String argument. ```Ruby "hello".ljust(4) #=> "hello" "hello".ljust(20) #=> "hello " "hello".ljust(20, '1234') #=> "hello123412341234123" ``` ```RBS class String def ljust: (Integer, ?String) -> String end ``` -------------------------------- ### Override Gemfile `require: false` for RBS Installation Source: https://github.com/ruby/rbs/blob/master/docs/collection.md Illustrates a scenario where a gem has `require: false` in `Gemfile`, but you still want `rbs collection` to install its RBS. This is achieved by explicitly setting `ignore: false` for that gem in `rbs_collection.yaml`. ```ruby # Gemfile gem 'GEM_NAME', require: false ``` ```yaml # rbs_collection.yaml gems: - name: GEM_NAME ignore: false ``` -------------------------------- ### Define Class Methods in RBS Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Illustrates how to define an RBS signature for a Ruby class method using `self.` prefix, exemplified by `Time.now` which returns a `Time` instance. ```Ruby Time.now # => 2009-06-24 12:39:54 +0900 ``` ```RBS class Time def self.now: () -> Time end ``` -------------------------------- ### RBS 3.9 Validation Examples Source: https://github.com/ruby/rbs/wiki/Release-Note-3.9 Showcases new validation rules introduced in RBS 3.9. These examples demonstrate how the type checker now rejects incorrect type applications, duplicate instance variable declarations, and invalid variable type validations, improving type safety and consistency. ```RBS class Foo < Array # Rejects incorrect type application @foo: String @foo: Integer # Rejects the duplicated instance variable declaration @bar: String[Integer] # Rejects variable type validation end ``` -------------------------------- ### Example Usage of rbs subtract Command Source: https://github.com/ruby/rbs/wiki/Release-Note-3.1 Demonstrates how to use `rbs subtract` to remove specific definitions from RBS files. This is particularly useful for managing conflicts between auto-generated and manually-written signatures. Examples include generating initial RBS files and then applying subtraction operations. ```sh # Generate RBSs for Active Record models under sig/rbs_rails directory $ bin/rake rbs_rails:all # Generate RBSs for all Ruby code under sig/prototype directory $ rbs prototype rb --out-dir=sig/prototype --base-dir=. app lib # Remove methods generated by RBS Rails from sig/prototype $ rbs subtract --write sig/prototype sig/rbs_rails # Remove hand-written methods from generated RBSs $ rbs subtract --write sig/prototype sig/rbs_rails sig/hand-written ``` -------------------------------- ### RBS::Environment API Updates and Utility Methods Source: https://github.com/ruby/rbs/wiki/Release-Note-3.0 This section outlines the recent changes and additions to the `RBS::Environment` class, including new entry types, updated method usages, and new utility methods for accessing environment data. It describes the replacement of generic entry types with specific ones and the introduction of `add_signature`. ```APIDOC RBS::Environment: Utilities and API Changes: - Entry Type Replacements: - `RBS::Environment::ModuleEntry` and `RBS::Environment::ClassEntry` are added, replacing usages of `MultiEntry[T]` - `RBS::Environment::ModuleAliasEntry`, `RBS::Environment::ClassAliasEntry`, `RBS::Environment::InterfaceEntry`, `RBS::Environment::ConstantEntry`, and `RBS::Environment::GlobalEntry` are added, replacing usage of `SingleEntry[N, T]` - Method Updates: - `add_signature(signature)`: Introduced to support `use` directives; use instead of `#<<` - `buffers_decls`: Moved to `RBS::Environment#signatures` - New Utility Methods: - `module_name?(name)` - `constant_entry(name)` - Recommendation: Use these methods instead of directly accessing internal data structures like `#class_decls` and `#type_alias_decls` (renamed from `#alias_decls`) ``` -------------------------------- ### Ruby Example: Using Enumerable#find with a Block Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Demonstrates a common Ruby usage pattern for `Enumerable#find` where the block can return a boolean or nil. This example highlights how such a block's return value is compatible with the `boolish` type in RBS, which accepts any object for conditional evaluation. ```ruby array.find {|x| x && x.some_test? } # The block will return (bool | nil) ``` -------------------------------- ### RBS Utility Classes Overview Source: https://github.com/ruby/rbs/blob/master/docs/architecture.md Provides a summary of various utility classes within the RBS library, detailing their specific functions such as type declaration validation, runtime type checking, unit test support for type definitions, and prototype generation from Ruby source code. ```APIDOC RBS::Validator: Provides validation for RBS type declarations, ensuring type name resolution and correct arity. RBS::Test: Offers runtime type checking to confirm if a Ruby object conforms to an RBS type, with integration for existing Ruby code. RBS::UnitTest: Supplies utilities for writing unit tests to verify consistency between RBS type definitions and Ruby implementations. RBS::Prototype: Core component for `rbs prototype` feature, scanning Ruby source or using reflection to generate RBS file prototypes. RBS::Collection: Includes features related to `rbs collection` functionality. ``` -------------------------------- ### RBS Instance and Class Variable Definition Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Illustrates how to define instance variables (`@name`), self-context instance variables (`self.@value`), and class variables (`@@instances`) in RBS. It shows the syntax for associating a type with each variable. ```RBS @name: String self.@value: Hash[Symbol, Key] @@instances: Array[instance] ``` -------------------------------- ### RBS Literal Type Examples Source: https://github.com/ruby/rbs/blob/master/docs/syntax.md Demonstrates various literal type definitions in RBS, including integer, string, symbol, and boolean literals, representing types with a single specific value. ```rbs 123 # Integer "hello world" # A string :to_s # A symbol true # true or false ``` -------------------------------- ### Install RBS 3.3 with Bundler Source: https://github.com/ruby/rbs/wiki/Release-Note-3.3 This snippet shows how to specify RBS version 3.3.0 in a Ruby project's Gemfile using Bundler. This ensures the correct version is installed and managed as a dependency, allowing developers to leverage the latest features and bug fixes. ```Ruby gem 'rbs', '~> 3.3.0' ``` -------------------------------- ### Example Ruby Type Assertion Tests for String Class Source: https://github.com/ruby/rbs/blob/master/docs/stdlib.md This Ruby code demonstrates how to write type assertion tests for the `String` class, covering both singleton methods (like `new`) and instance methods (like `gsub`). It utilizes `Test::Unit::TestCase` and `TypeAssertions` to verify method signatures and execution without exceptions. The `assert_send_type` method is used to assert expected input and output types for various method overloads. ```Ruby class StringSingletonTest < Test::Unit::TestCase include TypeAssertions testing "singleton(::String)" def test_initialize assert_send_type "() -> String", String, :new assert_send_type "(String) -> String", String, :new, "" assert_send_type "(String, encoding: Encoding) -> String", String, :new, "", encoding: Encoding::ASCII_8BIT assert_send_type "(String, encoding: Encoding, capacity: Integer) -> String", String, :new, "", encoding: Encoding::ASCII_8BIT, capacity: 123 assert_send_type "(encoding: Encoding, capacity: Integer) -> String", String, :new, encoding: Encoding::ASCII_8BIT, capacity: 123 assert_send_type "(ToStr) -> String", String, :new, ToStr.new("") assert_send_type "(encoding: ToStr) -> String", String, :new, encoding: ToStr.new('Shift_JIS') assert_send_type "(capacity: ToInt) -> String", String, :new, capacity: ToInt.new(123) end end class StringTest < Test::Unit::TestCase include TypeAssertions testing "::String" def test_gsub assert_send_type "(Regexp, String) -> String", "string", :gsub, /./, "" assert_send_type "(String, String) -> String", "string", :gsub, "a", "b" assert_send_type "(Regexp) { (String) -> String } -> String", "string", :gsub, /./ do |x| "" end assert_send_type "(Regexp) { (String) -> ToS } -> String", "string", :gsub, /./ do |x| ToS.new("") end assert_send_type "(Regexp, Hash[String, String]) -> String", "string", :gsub, /./, {"foo" => "bar"} assert_send_type "(Regexp) -> Enumerator[String, self]", "string", :gsub, /./ assert_send_type "(String) -> Enumerator[String, self]", "string", :gsub, "" assert_send_type "(ToStr, ToStr) -> String", "string", :gsub, ToStr.new("a"), ToStr.new("b") end end ``` -------------------------------- ### Generate RBS Signatures with `rbs prototype` Source: https://github.com/ruby/rbs/blob/master/README.md Shows how to use the `rbs prototype` command to automatically generate boilerplate RBS signature declarations from a Ruby source file, highlighting the initial untyped output that serves as a starting point for refinement. ```console $ rbs prototype rb person.rb class Person @name: untyped @contacts: untyped attr_reader name: untyped attr_reader contacts: untyped def initialize: (name: untyped) -> void def speak: () -> ::String end ``` -------------------------------- ### RBS::DefinitionBuilder Methods for Constructing Definitions Source: https://github.com/ruby/rbs/blob/master/docs/architecture.md Outlines the key methods of RBS::DefinitionBuilder used to construct RBS::Definition objects for different type contexts: singleton classes, instance types, and interfaces. It also mentions the internal use of AncestorBuilder and MethodBuilder. ```APIDOC RBS::DefinitionBuilder: build_singleton(class_or_module_name: TypeName) -> Definition Returns a definition for the singleton class of a given class or module. build_instance(type_name: TypeName) -> Definition Returns a definition for instances of a given class or module type. build_interface(interface_name: TypeName) -> Definition Returns a definition for a specified interface. ``` -------------------------------- ### Example Ruby Class Definition: Person Source: https://github.com/ruby/rbs/blob/master/README.md A simple Ruby class `Person` demonstrating `attr_reader` for properties, an `initialize` method with keyword arguments, and a regular instance method `speak`. ```ruby # person.rb class Person attr_reader :name attr_reader :contacts def initialize(name:) @name = name @contacts = [] end def speak "I'm #{@name} and I love Ruby!" end } ``` -------------------------------- ### RBS ReturnTypeError Example Source: https://github.com/ruby/rbs/blob/master/docs/sigs.md An example of a `ReturnTypeError` message, indicating that a method or block returned a value with an incorrect type. It shows the expected return type (`self`) versus the actual returned object. ```text ERROR -- : [Kaigi::Conference#each_speaker] ReturnTypeError: expected `self` but returns `[#]` ``` -------------------------------- ### RBS Method Overloading Syntax Source: https://github.com/ruby/rbs/blob/master/docs/sigs.md Demonstrates the correct syntax for defining overloaded methods in RBS. The `...` syntax is used in subsequent definitions to indicate that it's an overload, preventing `DuplicatedMethodDefinitionError`. ```rbs # First definition class C def foo: () -> untyped end # Second definition, use `...` syntax to tell RBS that we're overloading the method class C def foo: () -> untyped | ... end ``` -------------------------------- ### Configure RBS_TEST_OPT for Signature Loading Source: https://github.com/ruby/rbs/blob/master/docs/sigs.md This snippet demonstrates how to set the `RBS_TEST_OPT` environment variable to specify paths for loading RBS signatures. It typically includes `-I sig` for the default signature directory and `-r` flags for loading standard library modules like `pathname`, `set`, or `bigdecimal`. ```shell RBS_TEST_OPT='-r pathname -I sig' ``` -------------------------------- ### RBS 3.0 Parser and EnvironmentLoader API Changes Source: https://github.com/ruby/rbs/wiki/Release-Note-3.0 Outlines the breaking changes in `RBS::Parser` and `RBS::EnvironmentLoader` methods in RBS 3.0, including deprecated method overloads, new return types for `parse_signature`, and new/deleted methods. ```APIDOC RBS::Parser: - parse_method_type: Deprecated overloads with line: and column: keywords deleted - parse_type: Deprecated overloads with line: and column: keywords deleted - parse_signature: Now returns a triple of RBS::Buffer, Array[RBS::AST::Directives::t], and Array[RBS::AST::Declarations::t] RBS::EnvironmentLoader: - each_signature: Added - each_decl: Deleted ``` -------------------------------- ### Add RBS Collection Directory to Gitignore Source: https://github.com/ruby/rbs/blob/master/docs/collection.md Illustrates how to add the `.gem_rbs_collection/` directory to your project's `.gitignore` file to prevent the installed RBS files from being tracked by version control. ```console $ echo /.gem_rbs_collection/ >> .gitignore ``` -------------------------------- ### Install RBS Gem with Bundler Source: https://github.com/ruby/rbs/wiki/Release-Note-2.8 Demonstrates how to include the RBS gem version 2.8.0 in a Ruby project's Gemfile for Bundler-managed dependencies, ensuring the correct version is used. ```ruby gem 'rbs', '~> 2.8.0' ``` -------------------------------- ### Define RBS Class with Standard Member Order Source: https://github.com/ruby/rbs/blob/master/docs/CONTRIBUTING.md This RBS code snippet demonstrates the recommended standard order for members within a class definition. Adhering to this order (new/initialize, mixins, attributes, singleton methods, public methods, private methods) helps maintain consistency, improves readability, and simplifies diff analysis in version control. ```RBS class HelloWorld[X] def self.new: [A] () { (void) -> A } -> HelloWorld[A] # new or initialize comes first def initialize: () -> void include Enumerable[X, void] # Mixin comes next attr_reader language: (:ja | :en) # Attributes def self.all_languages: () -> Array[Symbol] # Singleton methods public # Public instance methods def each: () { (A) -> void } -> void # Members are sorted dictionary order def to_s: (?Locale) -> String private # Private instance methods alias validate validate_locale def validate_locale: () -> void end ``` -------------------------------- ### Declare Standard Library Dependencies in manifest.yaml Source: https://github.com/ruby/rbs/blob/master/docs/gem.md An example of a `manifest.yaml` file used to declare dependencies on standard Ruby libraries. These dependencies are typically not listed in the `.gemspec` but are necessary for the RBS gem to correctly resolve types. ```yaml dependencies: - name: json - name: logger - name: optparse - name: pathname - name: rdoc - name: tsort ``` -------------------------------- ### Implement Array#collect with Method Type Variables (Ruby/RBS) Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Illustrates the `Array#collect` method in Ruby, showcasing its element transformation capabilities. The RBS signature demonstrates the introduction of a method-level type variable `U` for the block's return type, and how the signature adapts for cases where a block is provided versus when an `Enumerator` is returned. ```ruby a = [ "a", "b", "c", "d" ] a.collect {|x| x + "!"} # => ["a!", "b!", "c!", "d!"] a.collect.with_index {|x, i| x * i} # => ["", "b", "cc", "ddd"] ``` ```rbs class Array[Elem] def collect: [U] () { (Elem) -> U } -> Array[U] | () -> Enumerator[Elem, Array[untyped]] end ``` -------------------------------- ### Example Ruby Test with `with_int` Helper for Union Types Source: https://github.com/ruby/rbs/blob/master/docs/stdlib.md This Ruby code snippet illustrates the use of the `with_int` helper, which is designed to yield values for each case of a given union type. This allows for comprehensive testing of methods that accept union types, ensuring `assert_send_type` can be used effectively with more complex type scenarios. ```Ruby def test_something with_int(3) do |int| # Yields twice with `Integer` and `ToInt` assert_send_type( "(int) -> Integer", some, :test, int ) end end ``` -------------------------------- ### Understand Annotation Inheritance for Method Aliases Source: https://github.com/ruby/rbs/wiki/Release-Note-3.9 This example clarifies the behavior of annotations when method aliases are created. Annotations applied directly to the `def` syntax of a method are not inherited by its aliases, while annotations attached to specific method overloads *are* inherited by aliases. ```RBS class Test %a{deprecated} def foo: %a{implicitly-returns-nil} () -> String alias bar foo end ``` -------------------------------- ### Define Implicit Dependencies in Manifest File Source: https://github.com/ruby/rbs/blob/master/docs/collection.md Shows an example of `manifest.yaml` for gem maintainers. This file is used to declare implicit dependencies, such as standard libraries (e.g., `pathname`), that are not explicitly listed in the gem's `Gemfile.lock` but are necessary for its RBS. ```yaml # manifest.yaml dependencies: # If your gem depends on pathname but the gemspec doesn't include pathname, # you need to write the following. - name: pathname ``` -------------------------------- ### Expanding Type Aliases with RBS::DefinitionBuilder Source: https://github.com/ruby/rbs/blob/master/docs/architecture.md Demonstrates the usage of DefinitionBuilder#expand_alias2 for a single-step unfolding of type aliases. This method provides a partial expansion, as full normalization is not supported due to the possibility of recursive alias definitions in RBS. ```ruby builder.expand_alias2(RBS::TypeName.parse("::int"), []) # => returns `::Integer | ::_ToInt` ``` -------------------------------- ### Testing `to_int` Methods with `RBS::UnitTest` Helpers Source: https://github.com/ruby/rbs/wiki/Release-Note-3.4 This Ruby example illustrates using `RBS::UnitTest`'s `with_int` helper to test methods that accept objects convertible to integers (e.g., via `#to_int`). This allows for more comprehensive type checking, ensuring methods work with various integer-like types beyond just primitive `Integer` objects. ```Ruby class YourGemClassTest < Minitest::Test def test_foo with_int(123) do |i| # `i` will be an Integer or an object with `#to_int` method assert_send_type( "(int) -> String", YourGemClass.new, :foo, i ) end end end ``` -------------------------------- ### Convert Enumerable to Hash using Tuples (Ruby/RBS) Source: https://github.com/ruby/rbs/blob/master/docs/rbs_by_example.md Explains how `Enumerable#to_h` in Ruby can construct a `Hash` from enumerable elements by utilizing a block that returns 2-item tuples. The RBS signature shows how type variables `T` and `U` are inferred from the tuple's elements to define the `Hash`'s key and value types. ```ruby (1..5).to_h {|x| [x, x ** 2]} # => {1=>1, 2=>4, 3=>9, 4=>16, 5=>25} ``` ```rbs class Enumerable[Elem] def to_h: () -> ::Hash[untyped, untyped] | [T, U] () { (Elem) -> [T, U] } -> ::Hash[T, U] end ``` -------------------------------- ### Example Output of Improved `rbs validate` Source: https://github.com/ruby/rbs/wiki/Release-Note-3.4 This code block displays the output from the enhanced `rbs validate` command. It demonstrates how the tool now reports multiple type definition errors, including the file, line number, and a highlighted section of the problematic code, rather than stopping at the first error. ```Shell $ rbs -I sig validate E, [2023-12-19T17:41:44.136289 #70306] ERROR -- rbs: sig/rbs.rbs:2:25...2:31: Could not find ::Logger (RBS::NoTypeFoundError) def self.logger: () -> Logger ^^^^^^ E, [2023-12-19T17:41:44.136354 #70306] ERROR -- rbs: sig/rbs.rbs:14:16...14:22: Could not find ::Logger (RBS::NoTypeFoundError) self.@logger: Logger? ^^^^^^ ... ```