簡體   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