Masataka Pocke Kuwabara
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
      • Invitee
    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Versions and GitHub Sync Engagement control Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
Invitee
Publish Note

Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

Your note will be visible on your profile and discoverable by anyone.
Your note is now live.
This note is visible on your profile and discoverable online.
Everyone on the web can find and read all notes of this public team.
See published notes
Unpublish note
Please check the box to agree to the Community Guidelines.
View profile
Engagement control
Commenting
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
  • Everyone
Suggest edit
Permission
Disabled Forbidden Owners Signed-in users Everyone
Enable
Permission
  • Forbidden
  • Owners
  • Signed-in users
Emoji Reply
Enable
Import from Dropbox Google Drive Gist Clipboard
   owned this note    owned this note      
Published Linked with GitHub
Subscribed
  • Any changes
    Be notified of any changes
  • Mention me
    Be notified of mention me
  • Unsubscribe
Subscribe
# Desgin Doc of rbs subtract This document describes rbs subtract. ## Basic Usage You can use `rbs subtract` from command line. ```console= # Print RBS to the stdout, which is generated.rbs - hand-written.rbs $ rbs subtract generated.rbs hand-written.rbs # It takes multiple minuends. The last argument becomes subtrahend. $ rbs subtract generated-a.rbs generated-b.rbs hand-written.rbs # It updates `.rbs` files directly with `-w` option. $ rbs subtract -w generated-a.rbs generated-b.rbs hand-written.rbs # It takes multiple subtrahends with --subtrahend option $ rbs subtract generated.rbs --subtrahend=hand-written-a.rbs --subtrahend=hand-written-b.rbs ``` ## The purpose of rbs subtract `rbs subtract` focuses on conbining auto-generated RBSs and hand-written RBSs (or two kinds auto-generated RBSs). There are several RBS generators. For example, `rbs prototype`, RBS Rails, and so on. They are useful, but the RBSs generated by them is not complete, for example, they include `untyped`. So we want to override the generated RBSs with hand-written RBSs. But we had no good way to override them. ```rbs= # auto-generated RBS class C # This definition doesn't describe the argument and returned value types, # so we want to describe them with hand-written RBS. def m: (untyped) -> untyped end # hand-written RBS class C # The following definition does not work with the generated RBS # because RBS doesn't allow duplicated method definitions. def m: (String) -> Integer # The following definition is valid, but it is not the expected behavior # because the overload still has `(untyped) -> untyped`. def m: (String) -> Integer | ... end ``` To solve this problem, we need to remove `C#m` definition from the generated RBS. But modifing a generated file by the hand introduces hard maintainability sooner or later. `rbs subtract` solves this problem. It removes duplicated definitions from the generated RBS automatically. Then we can maintain the generated RBSs. The `rbs subtract`'s goal is modifying generated RBSs to make it valid with the other RBSs. So, after `rbs subtract a.rbs b.rbs > c.rbs`, the environment including `b.rbs` and `c.rbs` has to be valid. ## Example workflow with rbs subtract We can use this command with the following workflow on a Rails application. ```bash= # 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 ``` Then the sig directory contains a complete RBS files as an environment. It means `rbs -Isig validate` passes (if there is no missing classes and so on). ## Detailed specifications See the test file. ## Implementation details The main implementation is `RBS::Subtractor`. It subtracts an environment from declarations. It uses `RBS::Environment` as the subtrahend. It needs to merge several class declarations for the same class, so `RBS::Declarations` is not appropriate for this purpose. The subtrahend RBSs is probably incomplete RBS, for example, it may depend on the minuend RBS. `RBS::DefinitionBuilder` does not work in this case, so it is inappropriate also. ## Limitations ### Interfaces mixin `rbs subtract` is not aware of interfaces mixins. For example ```rbs= # minuend - generated class C def x: () -> untyped end # subtrahend - hand-written class C include _I end interface _I def x: () -> untyped end # subtracted by `rbs subtract` class C def x: () -> untyped end ``` `x` method remains in the subtracted. Because it is actually defined by `_I`, but not `C`. It causes duplicated method definition error, so I'd like to improve this situation. #### Solution ideas * Remove entire of `class C` from subtracted if the subtrahend incldues interface mixins. * We can fix this problem easily. * But it may remove necessary methods. * Use DefinitionBuilder to trace inheritance * DefinitionBuilder builds inheritance, so we can remove methods defined by interface correctly. * But DefinitionBuilder needs complete RBS environment. * Search interface inheritance by the Subtractor * Re-implement DefinitionBuilder, but it works with incomplete environment. * It is bit of hard, and it doesn't 100% emulate the behavior. ### Different type parameters The subtracted RBS doesn't work with the subtrahend if the subtrahend contains a class/module with type parameters. ```rbs= # a.rbs class C def foo: () -> untyped end # b.rbs class C[T] def bar: () -> untyped end # rbs subtract a.rbs b.rbs is the following, the same as a.rbs # The type parameter of `C` is different, so it causes an error. class C def foo: () -> untyped end ``` ### attr_accessor Currently `rbs subtract` command removes `attr_accessor` if the subtrahend contains one of the methods that `attr_accessor` defines. For example ```rbs= # minuend.rbs class C # It defines a and a= attr_accessor a: String end # subtrahend.rbs class C def a: () -> String end ``` In this case, `rbs subtract a.rbs b.rbs` prints nothing. It removes `C#a=` unexpectedly. We can fix this problem more easily than other problems. We can convert `attr_accessor` to a `attr_{reader,writer}` in this case. ## Alternative Approaches This section describes alternative approaches that I considered. ### Specify multiple subtrahends #### Decided specification `rbs subtract` treat the last argument as a subtrahend by default. But you can also specify multiple subtrahends by `--subtrahend` option. For example: ```bash= # Specify one subtrahend $ rbs subtract minuend.rbs subtrahend.rbs # Specify two or more subtrahends $ rbs subtract minuend.rbs --subtrahend=subtrahend_1.rbs --subtrahend=subtrahend_2.rbs ``` #### Why this feature is necessary Specifying multiple subtrahends is useful on the following situaion. ``` . └── sig ├── app │   └── models/user.rbs ├── lib │   └── lib.rbs ├── prototype │   └── app/models/user.rbs └── rbs_rails └── app/moels/user.rbs 6 directories, 4 files ``` In this case, `rbs subtract` executes `(sig/prototype + sig/rbs_rails) - (sig/app + sig/lib)`, which takes two directories as the subtrahends. #### Alternative Solutions I considered the following solutions too. ```console= # Separate minuends and subtrahends by `-` # # It looks cool, but it is not common as CLI. # And I'm not sure I can implement it easily with optparse, because `-` is a meta character of optparse. $ rbs subtract sig/prototype sig/rbs_rails - sig/app sig/lib # Add --minuend option # # It is not bad, but I like --subtrahend. $ rbs subtract --minuend=sig/prototype --minuend=sig/rbs_rails sig/app sig/lib # Add --minuend and --subtrahend options # # It is too redundant. $ rbs subtract --minuend=sig/prototype --minuend=sig/rbs_rails \ --subtrahend=sig/app --subtrahend=sig/lib # Specify comma separated files as subtrahend # # I do not want to implement the comma separated files because of escaping comma. # It will introduce complexity. $ rbs subtract sig/prototype sig/rbs_rails sig/app,sig/lib ```

Import from clipboard

Paste your markdown or webpage here...

Advanced permission required

Your current role can only read. Ask the system administrator to acquire write and comment permission.

This team is disabled

Sorry, this team is disabled. You can't edit this note.

This note is locked

Sorry, only owner can edit this note.

Reach the limit

Sorry, you've reached the max length this note can be.
Please reduce the content or divide it to more notes, thank you!

Import from Gist

Import from Snippet

or

Export to Snippet

Are you sure?

Do you really want to delete this note?
All users will lose their connection.

Create a note from template

Create a note from template

Oops...
This template has been removed or transferred.
Upgrade
All
  • All
  • Team
No template.

Create a template

Upgrade

Delete template

Do you really want to delete this template?
Turn this template into a regular note and keep its content, versions, and comments.

This page need refresh

You have an incompatible client version.
Refresh to update.
New version available!
See releases notes here
Refresh to enjoy new features.
Your user state has changed.
Refresh to load new user state.

Sign in

Forgot password

or

By clicking below, you agree to our terms of service.

Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
Wallet ( )
Connect another wallet

New to HackMD? Sign up

Help

  • English
  • 中文
  • Français
  • Deutsch
  • 日本語
  • Español
  • Català
  • Ελληνικά
  • Português
  • italiano
  • Türkçe
  • Русский
  • Nederlands
  • hrvatski jezik
  • język polski
  • Українська
  • हिन्दी
  • svenska
  • Esperanto
  • dansk

Documents

Help & Tutorial

How to use Book mode

Slide Example

API Docs

Edit in VSCode

Install browser extension

Contacts

Feedback

Discord

Send us email

Resources

Releases

Pricing

Blog

Policy

Terms

Privacy

Cheatsheet

Syntax Example Reference
# Header Header 基本排版
- Unordered List
  • Unordered List
1. Ordered List
  1. Ordered List
- [ ] Todo List
  • Todo List
> Blockquote
Blockquote
**Bold font** Bold font
*Italics font* Italics font
~~Strikethrough~~ Strikethrough
19^th^ 19th
H~2~O H2O
++Inserted text++ Inserted text
==Marked text== Marked text
[link text](https:// "title") Link
![image alt](https:// "title") Image
`Code` Code 在筆記中貼入程式碼
```javascript
var i = 0;
```
var i = 0;
:smile: :smile: Emoji list
{%youtube youtube_id %} Externals
$L^aT_eX$ LaTeX
:::info
This is a alert area.
:::

This is a alert area.

Versions and GitHub Sync
Get Full History Access

  • Edit version name
  • Delete

revision author avatar     named on  

More Less

Note content is identical to the latest version.
Compare
    Choose a version
    No search result
    Version not found
Sign in to link this note to GitHub
Learn more
This note is not linked with GitHub
 

Feedback

Submission failed, please try again

Thanks for your support.

On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

Please give us some advice and help us improve HackMD.

 

Thanks for your feedback

Remove version name

Do you want to remove this version name and description?

Transfer ownership

Transfer to
    Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

      Link with GitHub

      Please authorize HackMD on GitHub
      • Please sign in to GitHub and install the HackMD app on your GitHub repo.
      • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
      Learn more  Sign in to GitHub

      Push the note to GitHub Push to GitHub Pull a file from GitHub

        Authorize again
       

      Choose which file to push to

      Select repo
      Refresh Authorize more repos
      Select branch
      Select file
      Select branch
      Choose version(s) to push
      • Save a new version and push
      • Choose from existing versions
      Include title and tags
      Available push count

      Pull from GitHub

       
      File from GitHub
      File from HackMD

      GitHub Link Settings

      File linked

      Linked by
      File path
      Last synced branch
      Available push count

      Danger Zone

      Unlink
      You will no longer receive notification when GitHub file changes after unlink.

      Syncing

      Push failed

      Push successfully