You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Fastlane中执行数值减法时出现String无`-`方法报错该如何解决?

问题修复方案

错误原因

报错是因为参与减法运算的两个值均为字符串类型,Ruby标准库中String类没有实现-运算方法,因此抛出undefined method '-' for "192":String (NoMethodError)异常:

  • ENV['BUILD_NUMBER']从环境变量读取的结果默认是字符串类型
  • 反引号执行git命令返回的结果是带末尾换行符的字符串,没有转为数值类型

修复步骤

  1. 调整BuildNumberFactory的make方法,将返回结果去空白后转为整数:
class BuildNumberFactory
  class << self
    def make
      `git rev-list HEAD --count`.strip.to_i
    end
  end
end
  1. 调整Fastfile中的lane逻辑,将环境变量取值也转为整数后再做运算:
lane :calculate_build_version do
  ENV['BUILD_NUMBER'].to_i - BuildNumberFactory.make
end

如果需要兼容BUILD_NUMBER环境变量为空的场景,可以补充默认值逻辑:

lane :calculate_build_version do
  (ENV['BUILD_NUMBER'] || 0).to_i - BuildNumberFactory.make
end

内容的提问来源于stack exchange,提问作者Sam Fisher

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.03 09:39:02