我正在试验 Ruby 3 中的一些 RBS 类型检查。我似乎无法使用rbs/stdlib
( https://github.com/ruby/rbs/tree/master/stdlib ) 中的类,例如 BigDecimal。
这个样本测试:
# file lib/user.rb
require 'bigdecimal'
class User
def initialize(name:, money:)
@name, @money = name, money
end
attr_reader :name, :money
def test(money)
puts "Hi #{name}, money=#{money}"
end
end
u = User.new(name: "John", money: BigDecimal(0, 0))
u.test(BigDecimal(10,0))
# file sig/user.rbs
class User
def initialize: (name: String name, money: BigDecimal money) -> untyped
attr_reader name: String
attr_reader money: BigDecimal
def test: (BigDecimal money) -> nil
end
运行检查后,我得到了错误Could not find BigDecimal (RBS::NoTypeFoundError)
。似乎 RBS 没有从 stdlib 加载 BigDecimal。
$ RBS_TEST_TARGET='User' RUBYOPT='-rrbs/test/setup' ruby lib/user.rb
I, [2021-04-19T12:05:25.376487 #14618] INFO -- : Setting up hooks for ::User
I, [2021-04-19T12:05:25.376576 #14618] INFO -- rbs: Installing runtime type checker in User...
I, [2021-04-19T12:05:25.377558 #14618] INFO -- : Setting up hooks for ::User
I, [2021-04-19T12:05:25.377581 #14618] INFO -- rbs: Installing runtime type checker in User...
/Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/errors.rb:104:in `check!': sig/user.rbs:6:21...6:31: Could not find BigDecimal (RBS::NoTypeFoundError)
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/variance_calculator.rb:114:in `type'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/variance_calculator.rb:83:in `in_method_type'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/definition_builder.rb:528:in `block (2 levels) in validate_type_params'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/definition_builder.rb:527:in `each'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/definition_builder.rb:527:in `block in validate_type_params'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/definition_builder/method_builder.rb:48:in `block in each'
from /usr/local/Cellar/ruby/3.0.1/lib/ruby/3.0.0/tsort.rb:350:in `block (2 levels) in each_strongly_connected_component'
from /usr/local/Cellar/ruby/3.0.1/lib/ruby/3.0.0/tsort.rb:431:in `each_strongly_connected_component_from'
from /usr/local/Cellar/ruby/3.0.1/lib/ruby/3.0.0/tsort.rb:349:in `block in each_strongly_connected_component'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/definition_builder/method_builder.rb:65:in `each_value'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/definition_builder/method_builder.rb:65:in `tsort_each_node'
from /usr/local/Cellar/ruby/3.0.1/lib/ruby/3.0.0/tsort.rb:347:in `call'
from /usr/local/Cellar/ruby/3.0.1/lib/ruby/3.0.0/tsort.rb:347:in `each_strongly_connected_component'
from /usr/local/Cellar/ruby/3.0.1/lib/ruby/3.0.0/tsort.rb:316:in `each_strongly_connected_component'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/definition_builder/method_builder.rb:43:in `each'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/definition_builder.rb:490:in `validate_type_params'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/definition_builder.rb:150:in `block (2 levels) in build_instance'
from <internal:kernel>:90:in `tap'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/definition_builder.rb:146:in `block in build_instance'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/definition_builder.rb:775:in `try_cache'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/definition_builder.rb:136:in `build_instance'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/test/tester.rb:45:in `install!'
from /Users/someone/.gem/ruby/3.0.1/gems/rbs-1.1.1/lib/rbs/test/setup.rb:69:in `block in <top (required)>'
from lib/user.rb:13:in `<class:User>'
from lib/user.rb:3:in `<main>'
我如何能够BigDecimal
在 RBS 中加载(和其他 stdlib 类型)?
谢谢。