Upgrade to Pro — share decks privately, control downloads, hide ads and more …

RBS and Rails, Present and Future

March 25, 2022

RBS and Rails, Present and Future


March 25, 2022

More Decks by pocke

Other Decks in Programming


  1. スライドトップと
 山田 太郎
 © Money Forward, Inc.

    RBS and Rails,
 Present and Future
 Masataka Pocke Kuwabara
 © Money Forward, Inc. 2022-03-25 銀座Rails#43 

  2. • Money Forward Osaka Office 
 • Live in Okayama

    • A maintainer of RBS
 I’m Masataka Pocke Kuwabara
 GitHub: @pocke
 © Money Forward, Inc.
  3. © Money Forward, Inc. • I work since Feb. 15

 • 50% for developing a Rails app, 50% for developing OSS (RBS)
 • Currently I'm working on updating Ruby 3.1 and introducing Steep to the Rails app.
 I joined Money Forward, inc!

  4. © Money Forward, Inc. • ruby/gem_rbs_collection#124: [activesupport] Remove Class#subclasses

    ruby/rbs#905: Remove CharScanner due to no longer used
 • ruby/rbs#907: Reduce memory usage of RBS::Buffer
 • kpumuk/meta-tags#237: Separate RBS files to _internal directory to avoid exposing RBS
 • ruby/rbs#914: Accept Numeric by Integer#{upto,downto}
 • ruby/rbs#919: Fix bin/sort for constants
 • rbs/pull#921: Add manifest.yaml for rbs gem
 • ruby/rbs#926: Fix bin/sort (WIP)
 • pocke/rbs_rails#212: CI against Ruby 3.1
 • pocke/rbs_rails#213: Bundle update to update rbs and steep
 • pocke/rbs_rails#214: Documentation update
 • pocke/rbs_rails#215: Fix spec
 • pocke/rbs_rails#216: Generate dependency types of relations
 • pocke/rbs_rails#217: Add rails generator to generate rbs.rake • pocke/rbs_rails#218: Do not expose polyfil RBSs
 My works

  5. © Money Forward, Inc. 
 I really recommend Kakioko

    (Oyster Okonomiyaki)🦪🦪🦪
 Go to Hinase (日生)!!!!!

  6. © Money Forward, Inc. • What is RBS
 • RBS

    and Rails (Present)
 • RBS and Rails (Future)
  7. © Money Forward, Inc. • "RBS is a language to

    describe the structure of Ruby programs."
 ◦ https://github.com/ruby/rbs#rbs
 • Type Language for Ruby
 • RBS is available since Ruby 2.6
 ◦ But I highly recommend Ruby 2.7+
 RBS is …

  8. © Money Forward, Inc. class String def gsub: (String |

    Regexp pat, String rep) -> String end RBS Syntax
 See https://github.com/ruby/rbs/blob/master/docs/rbs_by_exa mple.md as a tutorial, https://github.com/ruby/rbs/blob/master/docs/syntax.md as a reference.

  9. © Money Forward, Inc. • ruby/rbs
 • ruby/gem_rbs_collection
 • ruby/typeprof

    • soutaro/steep
 • pocke/rbs_rails
 Today I do not talk about Sorbet tools󰢛
 Related Projects

  10. Type Checkers
 © Money Forward, Inc. Related Projects


  11. © Money Forward, Inc. • You can use it with

    `gem install rbs`
 • Defines RBS language
 • Provides RBS loader for other tools
 • Provides tools to support using RBS
 • Contains core and stdlib's RBS files

  12. © Money Forward, Inc. • Provides gems' RBSs, such as

    activesupport gem
 • Similar with DefinitelyTyped for TypeScript
 • Contributions are welcome!
 ◦ https://github.com/ruby/gem_rbs_collection/blob/main /docs/CONTRIBUTING.md

  13. © Money Forward, Inc. • RBS files generator for Ruby

    on Rails
 • It focuses on generating methods generated by Rails.

  14. © Money Forward, Inc. How to introduce RBS to Rails

 1. Add gems to Gemfile
 2. Setup rbs collection
 3. Setup RBS Rails
 4. Setup Steep
 5. Run🚀

  15. © Money Forward, Inc. • Add rbs, steep, and rbs_rails

    gems to Gemfile
 • Then run `bundle install`
 Add gems to Gemfile

  16. © Money Forward, Inc. • rbs collection setups dependencies' RBSs.

    ◦ See my talk at RubyKaigi Takeout 2021 https://rubykaigi.org/2021-takeout/presentations/p_c k_.html
 • Run the following commands
 ◦ `rbs collection init`
 ◦ `rbs collection install`
 Setup rbs collection

  17. © Money Forward, Inc. • Run `rails g rbs_rails:install`

    Then, run `rake rbs_rails:all` to generate RBSs for AR models
 Setup RBS Rails

  18. © Money Forward, Inc. • Run `steep init`
 • Edit

 • Run `steep check`
 Setup & run Steep

  19. © Money Forward, Inc. • Use `steep langserver`
 ◦ https://github.com/soutaro/steep-vscode

    ◦ https://github.com/emacs-lsp/lsp-mode
 ◦ https://github.com/prabirshrestha/vim-lsp
 Using steep from editor

  20. Now we can use type checking with Steep! Is it

    ready to production?
 © Money Forward, Inc.
  21. © Money Forward, Inc. Currently I got 410 type errors

    on our Rails app, and it does not analyze whole of code.

  22. © Money Forward, Inc. • More accurate type checking

    Generating RBS
 ◦ Enhance RBS Rails
 ◦ Write more RBSs in gem_rbs_collection 
 ◦ Support meta-programming in RBS 
 • Introducing RBS to a team smoothly
 ◦ Run Steep on CI
 ◦ Improve performance 
 ◦ Generating RBS "continuously" 
 ◦ Share knowledge to team 

  23. © Money Forward, Inc. • More accurate type checking

    Generating RBS
 ◦ Enhance RBS Rails
 ◦ Write more RBSs in gem_rbs_collection 
 ◦ Support meta-programming in RBS 
 • Introducing RBS to a team smoothly
 ◦ Run Steep on CI
 ◦ Improve performance 
 ◦ Generating RBS "continuously" 
 ◦ Share knowledge to team 
 Issues (again)

  24. © Money Forward, Inc. • There are three command to

    generate RBS
 ◦ `rbs prototype rb`
 ◦ `rbs prototype runtime`
 ◦ `typeprof` (I don't talk today󰢛)
 Generating RBS: Current Behavior

  25. © Money Forward, Inc. • It parses `.rb` file, and

    generate RBS
 ◦ `rbs prototype rb path/to/file.rb`
 • 👍 Easy to run, easy to guess what will be generated
 • 👎 Not good at meta-programming
 ◦ e.g. eval, define_method, etc...
 rbs prototype rb

  26. © Money Forward, Inc. • It executes Ruby program, then

    extract method information with meta-programming
 ◦ `rbs prototype runtime KlassName`
 • 👍 Good at meta-programming
 • 👎 It may raise an error, generate dirty RBSs.
 • 👎 User needs to specify class name.
 rbs prototype runtime

  27. © Money Forward, Inc. • I recommend using `rbs prototype

 • But it is not the "Silver Bullet"
 Current state of generating RBS

  28. © Money Forward, Inc. • `rbs prototype rb` is not

    good at meta programming
 • It may generate RBS does not work standalone
 ◦ e.g. on `class C < D`, it generates C but doesn't generate D.
 • We still get type errors even if it runs with generated RBSs
 • It is not aware of existing RBSs

  29. © Money Forward, Inc. • For meta-programming
 ◦ → It

    should be based on `prototype runtime`
 • To work standalone
 ◦ → Fix bugs, generate dependency types
 • To avoid type errors
 ◦ → Generate more lenient types
 • To be aware of existing RBSs
 ◦ `rbs subtract` may solve this issue

  30. © Money Forward, Inc. • takahasim-san wrote an article about

    this problem few days ago (Japanese).
 ◦ Rails + RBS & Steepを試してみて困ったところとか感想 とか - Qiita
 • e.g. different self in lambda
 ◦ validate :foobar, if: -> { new_record? }
 Support meta-programming

  31. © Money Forward, Inc. • We can start introducing RBS

    easily, but it still has many problems.
 • I'm working on generating RBS. It should generate RBSs that work out of the box.
 • Go to Hinase, Eat Kakioko🦪🦪🦪