Ruby on Rails

Let's write RBS!

Let's write RBS!

by Masataka Kuwabara

In the video titled Let's write RBS! by Masataka Kuwabara at RubyKaigi 2023, the speaker discusses the RBS (Ruby Signature) project, focusing on new features introduced in RBS 3.1 and providing a demonstration on writing RBS. The session is structured into two main parts: an overview of the new RBS features and a practical demonstration of writing RBS in a Ruby application.

Key Points Discussed:
- Introduction of Contributors: Kuwabara acknowledges the contributors to the RBS project, emphasizing the community effort behind it.
- New Features in RBS 3.1:
- RBS Subtract Feature: This new feature allows users to remove duplicate method definitions across RBS files. It effectively takes the difference of two files, helping to manage consistency in method definitions when integrating RBS into existing Ruby applications.
- RBS Pass Command: Another new feature that parses RBS files to check for syntax errors. The introduction of a high query option enhances type checking without needing to write to a file.
- Demonstration Using VS Code: Kuwabara preps a demonstration where he introduces RBS to an existing Ruby application. The process includes setting up gems for type checking, initializing RBS settings, generating RBS files, and correcting type definitions within the application.
- Error Management and Types: The presenter emphasizes the importance of precisely defining types and managing errors effectively to improve code quality. This involves analyzing untyped definitions and specifying acceptable input types to enhance the robustness of the application.
- Final Thoughts: The talk wraps up with an invitation to further discussions on Ruby and open-source contributions. Kuwabara reinforces the functionalities of the RBS Subtract feature and its importance in dealing with redundant method definitions.

Conclusion:
Kuwabara's presentation effectively highlights the advancements made in RBS 3.1, particularly the RBS Subtract and RBS Pass features, and demonstrates their practical application in enhancing existing Ruby applications. The talk offers valuable insights into writing RBS and encourages participation in ongoing community discussions around Ruby development.

00:00:01.500 foreign
00:00:25.880 Hello everyone. I am an application engineer, and I also work on robust maintenance. I am not a threat...
00:00:33.300 Before this talk, I would like to introduce the RBS project contributors. This is a tribute to all the project contributors.
00:00:40.620 Thank you to these contributors.
00:00:54.860 So, today's agenda consists of two parts. First, I will introduce a new feature that has been added in RBS 3.1.
00:01:02.520 I originally planned to include Q&A, but I have limited time. In the second part, I will demonstrate how to write RBS.
00:01:11.340 I apologize, but I will not cover the basics of RBS, such as syntax and usage, because they are already documented. While I was writing this talk, I visited Ruby projects and wrote an article...
00:01:27.659 This article is available in Japanese, but it might be helpful for you.
00:01:40.500 Now, let’s talk about the new features introduced in RBS. First, I present the RBS Subtract feature. The RBS Subtract removes duplicate method definitions.
00:02:00.659 For instance, if you have two files, one is ARBS and the other is BRBS, the command removes duplicates in ARBS.
00:02:14.700 The result of the operation is effectively ARBS minus BRBS. This feature also supports overwrite options.
00:02:27.560 This feature was originally developed by Cytosan, and I took over from there. Thank you, Cytosan.
00:02:36.840 Let’s dive into the subtract behavior. Here is an example.
00:02:45.180 Consider a case where ARBS has the full method, and BRBS has just the method alone. If we run ARBS subtract BRBS, the output will contain only ARBS.
00:03:05.480 This command is necessary because it modifies or changes files in a meaningful way. Let’s consider a scenario where you are developing an existing Ruby application and introducing RBS. In this case, you might want to use an RBS generator.
00:03:40.980 You will need to write all of the RBS for your large application, which can be quite challenging. For instance, using an RBS generator will help facilitate the process.
00:04:07.620 The generated code may appear untyped.
00:04:12.299 You might think about editing the auto-generated code, but this is not a good idea as changes may be lost later.
00:04:34.440 We need to manage RBS files and generated database files separately, but this can lead to problems.
00:04:55.220 At least, you should avoid downloading duplicates. This can lead to inconsistent method definitions.
00:05:10.440 The solution to this issues is the RBS subtract command. It effectively removes duplication.
00:05:23.000 After executing RBS subtract, you will find that the resultant RBS and RBS Input files will have the same method definitions.
00:05:31.860 Here’s an example workflow demonstrating how to use this command.
00:05:45.220 First, we generate the application’s RBS, which saves prototype files in a common directory. The generated source should be from the app directory...
00:06:01.200 You may want to edit the generated file. It's possible to add a new file to handle it.
00:06:11.220 After this step, you have to remove duplicates from the other RBS files using the subtract command.
00:06:22.360 In such cases, you can use the subtract command to maintain consistency.
00:06:32.840 And once done, the RBS will be available for type checking with the generated library for your application.
00:06:45.240 For more information, this feature was implemented in a display case, and I've documented the design in detail.
00:06:58.000 I also shared a URL to Twitter along with a Wiki hashtag for further exploration.
00:07:10.980 The next feature is RBS pass. This command parses RBS files and checks for syntax errors.
00:07:30.000 For instance, an RBS file with no errors will be processed without issue, but if an error exists, it will be reported.
00:07:50.880 This feature has been available in RBS for a while, but I would like to introduce a new option to this command.
00:08:01.740 The new option is a high query, allowing for an even more precise type check.
00:08:19.500 With RBS 3.1, you no longer need to write to a file for type checks.
00:08:28.660 You only need to specify a string as an option from the command line.
00:08:37.819 The command will dynamically specify the context for passing syntax.
00:08:49.420 Although I won’t go into detail today due to time constraints, other tools added to the RBS collection can also assist.
00:09:01.620 Several tool features are also described in previous presentations or documentation.
00:09:12.440 Next, I will demonstrate writing RBS using VS Code with certain extensions.
00:09:26.500 However, you can also use other editors that support the LSP format to write RBS.
00:09:38.000 This link contains more tools and how to elevate RBS while providing several examples.
00:09:50.100 Today, I will showcase how I introduce RBS to an existing application called 'CI'. You may not be familiar with this repository, but it is a small implementation...
00:10:06.120 Here are the steps I took to demonstrate the service correction and how to write the ARBS files.
00:10:18.200 Let’s kick off this demonstration with the CI repository.
00:10:34.160 First, you need to set up some gems for type checking. The gems should be included in the development group.
00:10:43.200 RBS must also be listed under the dependencies.
00:10:52.080 After this, we accurately require the necessary features.
00:11:02.920 Once we’ve done that, you should install the gems with Bundler.
00:11:17.379 Now we have an RBS setting file. Let’s take a look at this file...
00:11:27.540 While editing today’s file is not required, configuration of where to download RBS and which gems to ignore is.
00:11:41.540 This serves as an initialization step after which we can install and run the RBS command.
00:11:55.180 Once we execute the commands, the RBS files are created.
00:12:06.060 The resulting structure will depend on the gem name and version.
00:12:18.300 The generated directory will be ignored and managed, so there is no need to version control it.
00:12:29.300 Our next step is to configure the Steep settings, which helps in checking the RBS.
00:12:41.560 We typically utilize the `steep init` command to generate this configuration file.
00:12:53.460 You need to specify the RBS directory and set the app directory as the location of the targets.
00:13:03.720 After setting up, make sure to commit your changes.
00:13:15.180 Following this, we generate the RBS files from the application code.
00:13:29.920 To achieve this, we may need to utilize several commands including RBS subtract.
00:13:43.080 Today, I’ve prepared a temporary directory for these experiments...
00:13:57.220 I’ll describe the processes involved with the temporary RBS file structure.
00:14:09.079 The first command you might find is the clean-up task, which just removes the alternatives.
00:14:20.660 Subsequently, corrections are executed, generating RBS files within the project directory.
00:14:33.680 Subtraction will allow resolution of any conflicting definitions.
00:14:49.540 Now, we perform the essential setup commands.
00:15:05.420 Once the commands are executed, our preparation is complete.
00:15:17.540 You'll see numerous errors due to the newly incorporated RBS files.
00:15:25.780 The target today is a specific file where we want to improve upon the existing type definitions.
00:15:38.860 We’ll need to address any untyped method definitions, such as this one in the file.
00:15:52.360 Selecting the appropriate method is essential to clarify its definitions.
00:16:05.220 In this case, the variable is typed correctly, thanks to the AWS libraries.
00:16:18.300 Once we resolve this, we need to add type definitions.
00:16:30.700 This method generally accepts a variety of inputs, from integers to other types.
00:16:44.080 The goal is to enforce stricter type checks.
00:16:57.560 We’ll attempt to reject undesired input while specifying what must be accepted.
00:17:09.820 This process is all about ensuring our types are precisely defined.
00:17:23.340 Now let's move onto error handling in methods.
00:17:38.340 We’ll analyze what exists in our current workspace.
00:17:50.200 Successfully running the necessary commands will help us finalize our RBS definitions.
00:18:00.780 We'll review and correct any remaining untyped definitions.
00:18:08.760 After we clean up, we’ll proceed to review and analyze the remaining methods and coordinates.
00:18:19.780 As you can see, the dynamic aspect of RBS types really assists with defining methods effectively.
00:18:29.440 After completing the revisions today, we’ll compile our findings.
00:18:39.420 In the final target, we will make sure all necessary operations are successful.
00:18:46.920 Thank you for listening to my talk.
00:18:51.820 I'd like to introduce a subsequent event.
00:19:00.300 The first is a discussion hosted by my fellow colleagues about Ruby, open-source software contributions, and community projects.
00:19:15.720 In this session, we encourage participation, and I invite everyone to join.
00:19:28.920 We also had a session yesterday discussing open-source contributions by well-known individuals.
00:19:42.120 So, if you are interested in further engagement, please attend our next event.
00:19:58.060 Finally, I would like to conclude my session.
00:20:08.300 Today, I shared the new features in RBS 3.1 including the RBS subtract functionality, as well as a demonstration on how to implement these changes.
00:20:24.620 Thank you for your attention!
00:20:37.540 Now for the implementation details on RBS subtract, I will quickly show you how this operates.
00:20:56.360 This involves the subtractor, a straightforward tool managing duplicate definitions.
00:21:05.420 The method recursively calls itself to effectively manage redundant entries.
00:21:20.680 During this process, it checks for existing definitions and records the relevant data.
00:21:35.900 Overall, it simplifies how we deal with redundant definitions across the board.
00:21:52.200 And with this, I appreciate your attentiveness throughout my presentation.
00:22:06.480 foreign