繁体   English   中英

Ruby:遇到Range作为字符串NoMethodError时,each_with_index失败

[英]Ruby: each_with_index bails when encountering Range as string, NoMethodError

我正在使用新的Postgresql功能,在我的Rails 4应用程序中存储数组。 如您所知,存储在PG中的数组的每个元素都是字符串。

所以我想要的数组= [7,8..9,10],但存储在PG中为{'7','8..9','10'}。 我可以测试每个元素,根据需要将其转换回整数或范围。

但是我收到一个NoMethodError:“ 8..9”的未定义方法'each_with_index':String

 array.each_with_index do |item, index|
   puts "index=#{index}, item=#{item}"
 end

我的schema.rb部分如下:

create_table "items", force: true do |t|
  t.string   "category"
  t.string   "sub_category"
  t.string   "name"
  t.string   "explanation"
  t.integer  "scoring"
  t.integer  "high_score"
  t.boolean  "all_or_nothing"
  t.datetime "created_at"
  t.datetime "updated_at"
  t.integer  "survey_id"
  t.boolean  "date"
  t.boolean  "time"
  t.string   "colors",         default: [], array: true
end

运行array.inspect会产生:

 "[\"7\", \" 8..9\", \" 10\"]"

阅读文档时,失败对我来说并不明显。 它不能用于字符串,因为它以前愉快地传递了“ 7”。 array [1] .class =>'String'。 我的Google搜索此特定情况的搜索为空。 我知道这是一个用字符串括起来的范围,但是在这里,它只是一个字符串?

为了完整性,了解我正在使用Windows和Ruby 2.0来构建此应用程序可能会有所帮助。

我应该在哪里了解如何解决此问题? 山姆,山姆

这可能是由于漫长的工作日结束,但是在第二天重新启动后,我无法重现此现象。 必须说在此过程中我已经重新启动了控制台,但是显然我需要做更多的事情才能重新启动Windows中的整个环境。 由于mu太短了,无法与我一起迟到,无法解决这种一次性行为。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM