Share
Embed
RBS and Rails, Present and Future
At Ginza Rails #43
https://ginza-rails.connpass.com/event/242685/
pocke
March 25, 2022
Transcript
47 slides
-
スライドトップと してご利用ください マネーフォワード事業本部 山田 太郎 © Money Forward, Inc.
RBS and Rails, Present and Future Masataka Pocke Kuwabara © Money Forward, Inc. 2022-03-25 銀座Rails#43 -
• Money Forward Osaka Office • Live in Okayama
• A maintainer of RBS I’m Masataka Pocke Kuwabara GitHub: @pocke © Money Forward, Inc. -
© 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! -
© 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 -
© Money Forward, Inc. I really recommend Kakioko
(Oyster Okonomiyaki)🦪🦪🦪 Go to Hinase (日生)!!!!! -
© Money Forward, Inc. • What is RBS • RBS
and Rails (Present) • RBS and Rails (Future) Agenda -
© 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 … -
© 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. -
© 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 -
Type Checkers Language © Money Forward, Inc. Related Projects ruby/
rbs ruby/ gem_rbs_ collection soutaro/ steep ruby/ typeprof Tool pocke/ rbs_rails Use Generate -
© 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 -
© 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 -
© Money Forward, Inc. • A type checker for Ruby
ruby/typeprof -
© Money Forward, Inc. • A type checker for Ruby
soutaro/steep -
© Money Forward, Inc. Difference between Steep and TypeProf https://www.slideshare.net/mametter/typeprof-for-ide-enrich-developm
ent-experience-without-annotations -
© Money Forward, Inc. • RBS files generator for Ruby
on Rails • It focuses on generating methods generated by Rails. pocke/rbs_rails -
© 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🚀 -
© Money Forward, Inc. • Add rbs, steep, and rbs_rails
gems to Gemfile • Then run `bundle install` Add gems to Gemfile -
© 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 -
© Money Forward, Inc. • Run `rails g rbs_rails:install` •
Then, run `rake rbs_rails:all` to generate RBSs for AR models Setup RBS Rails -
© Money Forward, Inc. • Run `steep init` • Edit
Steepfile • Run `steep check` Setup & run Steep -
© 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 -
Now we can use type checking with Steep! Is it
ready to production? © Money Forward, Inc. -
© Money Forward, Inc. Currently I got 410 type errors
on our Rails app, and it does not analyze whole of code. -
© 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 -
© 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) -
© Money Forward, Inc. Generating RBS (continuously) 1. Current Behavior
2. Problem 3. Goal -
© 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 -
© 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 -
© 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 -
© Money Forward, Inc. • I recommend using `rbs prototype
rb` • But it is not the "Silver Bullet" Why? Current state of generating RBS -
© 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 -
© 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 -
© 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 -
© 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