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

RBS and Rails, Present and Future

pocke
March 25, 2022

RBS and Rails, Present and Future

pocke

March 25, 2022
Tweet

More Decks by pocke

Other Decks in Programming

Transcript

  1. スライドトップと

    してご利用ください

    マネーフォワード事業本部 

    山田 太郎

    © Money Forward, Inc.
    RBS and Rails,

    Present and Future

    Masataka Pocke Kuwabara

    © Money Forward, Inc.
    2022-03-25 銀座Rails#43 


    View full-size slide

  2. ● Money Forward Osaka Office

    ● Live in Okayama

    ● A maintainer of RBS

    I’m Masataka Pocke
    Kuwabara

    GitHub: @pocke

    © Money Forward, Inc.

    View full-size slide

  3. © Money Forward, Inc.
    ● I work since Feb. 15 2022

    ● 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!


    View full-size slide

  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


    View full-size slide

  5. © Money Forward, Inc.


    #okayama in our slack (1/2)


    View full-size slide

  6. © Money Forward, Inc.


    #okayama in our slack (1/2)


    View full-size slide

  7. © Money Forward, Inc.


    I really recommend Kakioko (Oyster
    Okonomiyaki)🦪🦪🦪

    Go to Hinase (日生)!!!!!


    View full-size slide

  8. © Money Forward, Inc.
    Agenda

    View full-size slide

  9. © Money Forward, Inc.
    ● What is RBS

    ● RBS and Rails (Present)

    ● RBS and Rails (Future)

    Agenda

    View full-size slide

  10. © Money Forward, Inc.
    What is RBS

    View full-size slide

  11. © 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 …


    View full-size slide

  12. © 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.


    View full-size slide

  13. © 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


    View full-size slide

  14. Type Checkers
 Language

    © Money Forward, Inc.
    Related Projects

    ruby/

    rbs

    ruby/

    gem_rbs_

    collection

    soutaro/

    steep

    ruby/

    typeprof

    Tool

    pocke/

    rbs_rails

    Use
 Generate


    View full-size slide

  15. © 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

    ruby/rbs


    View full-size slide

  16. © 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

    ruby/gem_rbs_collection


    View full-size slide

  17. © Money Forward, Inc.
    ● A type checker for Ruby

    ruby/typeprof


    View full-size slide

  18. © Money Forward, Inc.
    ● A type checker for Ruby

    soutaro/steep


    View full-size slide

  19. © Money Forward, Inc.
    Difference between Steep and
    TypeProf

    https://www.slideshare.net/mametter/typeprof-for-ide-enrich-developm
    ent-experience-without-annotations 


    View full-size slide

  20. © Money Forward, Inc.
    ● RBS files generator for Ruby on Rails

    ● It focuses on generating methods generated
    by Rails.

    pocke/rbs_rails


    View full-size slide

  21. © Money Forward, Inc.
    RBS and Rails
    (Present)

    View full-size slide

  22. © Money Forward, Inc.
    How to introduce RBS
    to Rails app

    1. Add gems to Gemfile

    2. Setup rbs collection

    3. Setup RBS Rails

    4. Setup Steep

    5. Run🚀


    View full-size slide

  23. © Money Forward, Inc.
    ● Add rbs, steep, and rbs_rails gems to Gemfile

    ● Then run `bundle install`

    Add gems to Gemfile


    View full-size slide

  24. © 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


    View full-size slide

  25. © Money Forward, Inc.
    ● Run `rails g rbs_rails:install`

    ● Then, run `rake rbs_rails:all` to generate
    RBSs for AR models

    Setup RBS Rails


    View full-size slide

  26. © Money Forward, Inc.
    ● Run `steep init`

    ● Edit Steepfile

    ● Run `steep check`

    Setup & run Steep


    View full-size slide

  27. © 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


    View full-size slide

  28. Now we can use type checking
    with Steep! Is it ready to
    production?

    © Money Forward, Inc.

    View full-size slide

  29. No

    © Money Forward, Inc.

    View full-size slide

  30. © Money Forward, Inc.
    Currently I got 410 type errors on our Rails app,
    and it does not analyze whole of code.


    View full-size slide

  31. © 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


    View full-size slide

  32. © Money Forward, Inc.
    RBS and Rails
    (Future)

    View full-size slide

  33. © 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)


    View full-size slide

  34. © Money Forward, Inc.
    Generating RBS
    (continuously)

    1. Current Behavior

    2. Problem

    3. Goal


    View full-size slide

  35. © 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


    View full-size slide

  36. © 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


    View full-size slide

  37. © 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


    View full-size slide

  38. © Money Forward, Inc.
    ● I recommend using `rbs prototype rb`

    ● But it is not the "Silver Bullet"


    Why?

    Current state of generating RBS


    View full-size slide

  39. © 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

    Problems


    View full-size slide

  40. © 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

    Goals


    View full-size slide

  41. © Money Forward, Inc.
    Support
    meta-programming


    View full-size slide

  42. © 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


    View full-size slide

  43. © Money Forward, Inc.
    Conclusion

    View full-size slide

  44. © 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🦪🦪🦪

    Conclusion


    View full-size slide

  45. ©Money Forward, Inc.
    We are hiring!


    View full-size slide