ruby-on-rails - 构建哈希时字符串化键错误

标签 ruby-on-rails ruby hash

我收到一个 stringify_keys 错误。

目前我正在调用以下完美运行的方法:

def attributes
  {
    city:         @content[1..20].strip,
    streetname:   @content[21..40].strip,
    house_number: @content[41..46].strip.to_i
  }
end

现在我正在重构我的代码,我需要从头开始构建哈希,其中键和值根据特定条件(条件尚未写入)填充哈希。

def attributes
  test = {}
  test["city"]          = @content[1..20].strip
  test["streetname"]    = @content[21..40].strip
  test["house_number"]  = @content[41..46].strip.to_i
end

现在我收到了 stringify_keys 错误。我查看了文档以获取有关如何构建哈希的线索,但没有任何可以帮助我的东西。

问题出在哪里?如果您需要更多代码,请询问。

最佳答案

第一段代码的关键是symbol,第二段代码最后要返回test

def attributes
  test = {}
  test[:city]          = @content[1..20].strip
  test[:streetname]    = @content[21..40].strip
  test[:house_number]  = @content[41..46].strip.to_i
  test
end

关于ruby-on-rails - 构建哈希时字符串化键错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23489378/

相关文章:

ruby-on-rails - Rails - OmniAuth-Facebook 抛出 NoSessionError

ruby-on-rails - 使用记录器录制 mp3 会创建一个 blob。但是当发送到服务器时,它的播放时间为 0 秒

ruby-on-rails - rails 3 : assets pipeline + many layouts

Ruby Shoes 包装器,包括应用程序中的 Shoes

ruby - RMagick:获取字形指标

python - 32 字节数据的最短编码

hash - 在终端中生成盐渍哈希

ruby-on-rails - Google 日历与初始 time_max 增量同步

php - 用于部署的语言/堆栈

swift - 在 Swift 中实现哈希组合器