DEV Community ๐Ÿ‘ฉโ€๐Ÿ’ป๐Ÿ‘จโ€๐Ÿ’ป

Yuta Goto
Yuta Goto

Posted on

hello gem steep

Ruby3.0ใ‹ใ‚‰้™็š„ๅž‹ใƒใ‚งใƒƒใ‚ฏใ™ใ‚‹ๆฉŸ่ƒฝใŒ่ฟฝๅŠ ใ•ใ‚Œใ‚‹ใจใ„ใ†ใ“ใจใงใ€่ปฝใ่งฆใฃใŸใฎใงๅ‚™ๅฟ˜้Œฒใงใ™ใ€‚

Steepใจใ„ใ†Gemใ‚’ไฝฟใ„ใพใ—ใŸใ€‚

GitHub logo soutaro / steep

Static type checker for Ruby

Steep - Gradual Typing for Ruby

Installation

Install via RubyGems.

$ gem install steep

Requirements

Steep requires Ruby 2.6 or later.

Usage

Steep does not infer types from Ruby programs, but requires declaring types and writing annotations You have to go on the following three steps.

0. steep init

Run steep init to generate a configuration file.

$ steep init       # Generates Steepfile

Edit the Steepfile:

target :app do
  check "lib"
  signature "sig"
  library "set", "pathname"
end
Enter fullscreen mode Exit fullscreen mode

1. Declare Types

Declare types in .rbs files in sig directory.

class Person
  @name: String
  @contacts: Array[Email | Phone]
  def initialize: (name: String) -> untyped
  def name: -> String
  def contacts: -> Array[Email | Phone]
  def guess_country: -> (String | nil)
end

class Email
  @address: String

  def initialize: (address: String) -> untyped
  def address: -> String
end

class Phone
  @country: String
  @number: String

  def initialize: (country: String, number: String) -> untyped
โ€ฆ

็ทด็ฟ’ใƒชใƒใ‚ธใƒˆใƒช


็ตๆžœ

ใ‚„ใฃใฆใ„ใ‚‹ใ“ใจ

lib/ ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซใ„ใคใ‚‚ใฉใŠใ‚Šใซใ‚ฏใƒฉใ‚นใ‚’ๅฎš็พฉใ—ใพใ™ใ€‚

# lib/hello.rb
class Hello
  def initialize(name)
    @name = name
  end

  def say
    "hello, #{@name}"
  end
end
Enter fullscreen mode Exit fullscreen mode

sig/ ใƒ‡ใ‚ฃใƒฌใ‚ฏใƒˆใƒชใซๅž‹ใฎๅฎš็พฉใ‚’ๆ›ธใ„ใฆใ„ใใพใ™ใ€‚ๆ‹กๅผตๅญใฏ .rbi ใซใชใ‚Šใพใ™ใ€‚

# sig/hello.rbi
class Hello < Object
  @name: String

  def initialize: (String) -> any
  def say: -> String
end
Enter fullscreen mode Exit fullscreen mode

ใƒกใ‚คใƒณใงไฝฟใฃใฆใฟใพใ™ใ€‚

# app.rb
require "./lib/hello"

hello1 = Hello.new('John')
puts hello1.say

hello2 = Hello.new(123)
puts hello2.say
Enter fullscreen mode Exit fullscreen mode

ใ“ใฎ app.rb ใฏ hello2 ใซไปฃๅ…ฅใ™ใ‚‹ใจใใฎ Hello ใ‚’ initialize ใ™ใ‚‹ใจใใฎๅผ•ๆ•ฐใฎๅž‹ใŒ้•ใฃใฆใ„ใพใ™ใ€‚ใŸใ ใ“ใฎใพใพๅฎŸ่กŒใ—ใฆใ‚‚ใจใใซใ‚จใƒฉใƒผใ‚‚ใชใๅฎŸ่กŒใ™ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚
ใ“ใฎใ‚ใŸใ‚Šใ‚‚ใƒใ‚งใƒƒใ‚ฏใ•ใ›ใ‚‹ใ“ใจใŒใงใใพใ™ใ€‚

$ bundle exec steep check --fallback-any-is-error app.rb
app.rb:6:19: ArgumentTypeMismatch: receiver=::Hello.class constructor, expected=::String, actual=::Integer (123)
Enter fullscreen mode Exit fullscreen mode

6่กŒ็›ฎใฎใจใ“ใ‚ใงๅผ•ๆ•ฐใฎๅž‹ใŒ้•ใ†ใ“ใจใ‚’ๆŒ‡ๆ‘˜ใ—ใฆใใ‚ŒใŸใฎใงใ€ๆˆๅŠŸใงใ™ใ€‚

ๆ‰€็ฎก

ใ‚ทใƒณใƒ—ใƒซใชใ‚ฏใƒฉใ‚นๅฎš็พฉใชใฉใงใฏใ‹ใชใ‚Š็ฐกๅ˜ใซไฝฟใ†ใ“ใจใŒใงใใฆใ€ใจใฆใ‚‚ใ‚ˆใ•ใใ†ใงใ™ใ€‚
Railsใฎใƒขใƒ‡ใƒซใƒกใ‚ฝใƒƒใƒ‰ใ‚„Serviceใ‚ฏใƒฉใ‚นใชใฉ้ƒจๅˆ†็š„ใซใ‚‚Railsใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใซๅ–ใ‚Š่พผใ‚ใใ†ใงใกใ‚‡ใฃใจๅž‹ๅฎ‰ๅ…จใซใชใ‚‹ใ‹ใ‚‚ใ—ใ‚Œใพใ›ใ‚“ใ€‚

Top comments (0)

๐ŸŒš Turn on dark mode

๐Ÿ”  Change your default font

๐Ÿ“š Adjust your experience level to see more relevant content

ย 

Head to your account's Settings to do all this and more.