Enjoy Ruby Programming
in IDE and TypeProf
Yusuke Endoh (@mametter)
RubyConf 2021
Yusuke Endoh / @mametter
•A Ruby committer working at Cookpad w/ @ko1
•Main contribution so far:
• Designed and implemented keyword arguments
• Implemented test coverage feature
• Implementing TypeProf  Today's topic
2
A recent contribution: error_highlight
3
json = { foo: { bar: { baz: 42 } } }
json[:foo][:barr][:baz]
# Ruby 3.0
$ ruby t.rb
t.rb:2:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)
# Ruby 3.1
$ ruby t.rb
t.rb:2:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError)
json[:foo][:barr][:baz]
^^^^^^
Credit: The original author of its prototype is @yui-knk
https://github.com/ruby/ruby/pull/4470
Index
•What is TypeProf?
•TypeProf for IDE
•How to use TypeProf for IDE
•Conclusion
4
TypeProf: A static analyzer for Ruby 3
•TypeProf analyzes non-annotated Ruby code
• guess types of method arguments and a return value
5
class User
def initialize(name)
@name = name
end
end
User.new("John")
Ruby code
class User
def initialize:(String name)->void
end
RBS
TypeProf
TypeProf? RBS? Steep? Sorbet?
6
Name What
RBS
TypeProf
Steep
Sorbet
The Ruby official type definition language
A static type analyzer for Ruby
A static type analyzer for Ruby
A static type analyzer for Ruby
Comparison between static analyzers
7
TypeProf Steep Sorbet
Type inference Strong Weak Weak
Accuracy Weak Strong Strong
Analysis speed Slow Fast Very fast
RBS support Yes Yes Not yet
IDE support No → Yes Yes Yes
RBS: An official language for Ruby types
• Common complaint: "Header files suck"
• TypeProf for IDE may solve this issue
8
class User
def initialize(name)
@name = name
end
end
User.new("John")
user.rb
class User
def initialize:
(String name) -> void
end
user.rbs
Index
•What is TypeProf?
•TypeProf for IDE
•How to use TypeProf for IDE
•Conclusion
9
Today's topic: TypeProf for IDE
10
VSCode TypeProf
code changed
error found
complete "5.ti"
maybe "5.times"
5.ti|
Do you mean:
5.times
1 + "str"
1 + "str"
Is this a bug?
• A VSCode extension for Ruby powered by TypeProf
Demo: TypeProf for IDE
11
The modern development experience
with TypeProf for IDE
12
On-the-fly method signature
The modern development experience
with TypeProf for IDE
13
On-the-fly error reporting
The modern development experience
with TypeProf for IDE
14
The modern development experience
with TypeProf for IDE
15
On-the-fly type inference
Completion
The modern development experience
with TypeProf for IDE
16
Hint for arguments
Demo: Summary
•Modern development experience comes to Ruby
without type annotations
•RBS are interspersed to Ruby files
• One possible answer to "Header files suck"
17
Index
•What is TypeProf?
•TypeProf for IDE
•How to use TypeProf for IDE
•Conclusion
18
How to configure TypeProf for IDE
• Use ruby 3.1.0-dev (development version☺)
• Add for your Gemfile
• Configure RBS collection
• if you use gems
• Install VSCode extension→
• search "typeprof"
• Open your folder with VSCode and pray
• More detail: https://github.com/ruby/typeprof/blob/master/doc/ide.md
19
gem "typeprof"
Protips™
•Write a simple test in a file
•Write RBS 😞
20
Demo: Tests instead of type annotations
21
untyped
String
Demo: Passing an unknown type
22
The method signature is changed
to accept a new type
Error in the callee side
Demo: Manual RBS specification
23
Click here
A prototype RBS is
created
Demo: Manual RBS specification (cont'd)
24
Error in the caller side
# means RBS-defined
Protips™
•Write a simple test in a file
• TypeProf requires a test to guess method signatures
•Write RBS to fix method signatures (if needed)
• This makes TypeProf analysis faster
• This is also useful to make TypeProf faster
25
Index
•What is TypeProf?
•TypeProf for IDE
•How to use TypeProf for IDE
•Conclusion
26
Release plan
•TypeProf for IDE will be released in Ruby 3.1
• Happy if you could play with it and give us feedback
•Ready for production?
• Experimental, but hopefully works for small programs
• For large code base, please write RBS for gems first!
• https://github.com/ruby/gem_rbs_collection
27
Special thanks
•Hideki Miura
•Ruby committers: matz, akr, ko1, soutaro
•Katsuhiro Ueno & Eijiro Sumii
•Stripe team & Shopify team & Jeff Foster
•Yuta Saito (@kateinoigakukun)
• Many improvements of TypeProf for IDE
28
Conclusion
•The modern development experience is possible
without full type annotations by TypeProf for IDE
•Ruby 3.1 will bundle TypeProf for IDE
29

Enjoy Ruby Programming in IDE and TypeProf

  • 1.
    Enjoy Ruby Programming inIDE and TypeProf Yusuke Endoh (@mametter) RubyConf 2021
  • 2.
    Yusuke Endoh /@mametter •A Ruby committer working at Cookpad w/ @ko1 •Main contribution so far: • Designed and implemented keyword arguments • Implemented test coverage feature • Implementing TypeProf  Today's topic 2
  • 3.
    A recent contribution:error_highlight 3 json = { foo: { bar: { baz: 42 } } } json[:foo][:barr][:baz] # Ruby 3.0 $ ruby t.rb t.rb:2:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError) # Ruby 3.1 $ ruby t.rb t.rb:2:in `<main>': undefined method `[]' for nil:NilClass (NoMethodError) json[:foo][:barr][:baz] ^^^^^^ Credit: The original author of its prototype is @yui-knk https://github.com/ruby/ruby/pull/4470
  • 4.
    Index •What is TypeProf? •TypeProffor IDE •How to use TypeProf for IDE •Conclusion 4
  • 5.
    TypeProf: A staticanalyzer for Ruby 3 •TypeProf analyzes non-annotated Ruby code • guess types of method arguments and a return value 5 class User def initialize(name) @name = name end end User.new("John") Ruby code class User def initialize:(String name)->void end RBS TypeProf
  • 6.
    TypeProf? RBS? Steep?Sorbet? 6 Name What RBS TypeProf Steep Sorbet The Ruby official type definition language A static type analyzer for Ruby A static type analyzer for Ruby A static type analyzer for Ruby
  • 7.
    Comparison between staticanalyzers 7 TypeProf Steep Sorbet Type inference Strong Weak Weak Accuracy Weak Strong Strong Analysis speed Slow Fast Very fast RBS support Yes Yes Not yet IDE support No → Yes Yes Yes
  • 8.
    RBS: An officiallanguage for Ruby types • Common complaint: "Header files suck" • TypeProf for IDE may solve this issue 8 class User def initialize(name) @name = name end end User.new("John") user.rb class User def initialize: (String name) -> void end user.rbs
  • 9.
    Index •What is TypeProf? •TypeProffor IDE •How to use TypeProf for IDE •Conclusion 9
  • 10.
    Today's topic: TypeProffor IDE 10 VSCode TypeProf code changed error found complete "5.ti" maybe "5.times" 5.ti| Do you mean: 5.times 1 + "str" 1 + "str" Is this a bug? • A VSCode extension for Ruby powered by TypeProf
  • 11.
  • 12.
    The modern developmentexperience with TypeProf for IDE 12 On-the-fly method signature
  • 13.
    The modern developmentexperience with TypeProf for IDE 13 On-the-fly error reporting
  • 14.
    The modern developmentexperience with TypeProf for IDE 14
  • 15.
    The modern developmentexperience with TypeProf for IDE 15 On-the-fly type inference Completion
  • 16.
    The modern developmentexperience with TypeProf for IDE 16 Hint for arguments
  • 17.
    Demo: Summary •Modern developmentexperience comes to Ruby without type annotations •RBS are interspersed to Ruby files • One possible answer to "Header files suck" 17
  • 18.
    Index •What is TypeProf? •TypeProffor IDE •How to use TypeProf for IDE •Conclusion 18
  • 19.
    How to configureTypeProf for IDE • Use ruby 3.1.0-dev (development version☺) • Add for your Gemfile • Configure RBS collection • if you use gems • Install VSCode extension→ • search "typeprof" • Open your folder with VSCode and pray • More detail: https://github.com/ruby/typeprof/blob/master/doc/ide.md 19 gem "typeprof"
  • 20.
    Protips™ •Write a simpletest in a file •Write RBS 😞 20
  • 21.
    Demo: Tests insteadof type annotations 21 untyped String
  • 22.
    Demo: Passing anunknown type 22 The method signature is changed to accept a new type Error in the callee side
  • 23.
    Demo: Manual RBSspecification 23 Click here A prototype RBS is created
  • 24.
    Demo: Manual RBSspecification (cont'd) 24 Error in the caller side # means RBS-defined
  • 25.
    Protips™ •Write a simpletest in a file • TypeProf requires a test to guess method signatures •Write RBS to fix method signatures (if needed) • This makes TypeProf analysis faster • This is also useful to make TypeProf faster 25
  • 26.
    Index •What is TypeProf? •TypeProffor IDE •How to use TypeProf for IDE •Conclusion 26
  • 27.
    Release plan •TypeProf forIDE will be released in Ruby 3.1 • Happy if you could play with it and give us feedback •Ready for production? • Experimental, but hopefully works for small programs • For large code base, please write RBS for gems first! • https://github.com/ruby/gem_rbs_collection 27
  • 28.
    Special thanks •Hideki Miura •Rubycommitters: matz, akr, ko1, soutaro •Katsuhiro Ueno & Eijiro Sumii •Stripe team & Shopify team & Jeff Foster •Yuta Saito (@kateinoigakukun) • Many improvements of TypeProf for IDE 28
  • 29.
    Conclusion •The modern developmentexperience is possible without full type annotations by TypeProf for IDE •Ruby 3.1 will bundle TypeProf for IDE 29