如何通过索引访问体式集合中的项目

How to access an item in an asana collection by index

我正在尝试更新将 trello 卡片导入 asana 的代码,我不得不更新到最新的 asana gem https://github.com/Asana/ruby-asana.

现在我在尝试按索引访问集合项时遇到此错误。

exportTrelloToAsana.rb:63:in `block in <main>': undefined method `[]' for #<Asana::Resources::Collection:0x007fbba1d0d810> (NoMethodError)
    from exportTrelloToAsana.rb:56:in `each'
    from exportTrelloToAsana.rb:56:in `<main>'

按索引访问 Asana::Resources::Collection 的正确语法是什么?

client = Asana::Client.new do |c|
  c.authentication :access_token, PERSONAL_ACCESS_TOKEN
end

workspaces = client.workspaces.find_all
workspace = workspaces[0]

正如您在 README.md the library will return a Asana::Resources::Collection 中提到的 API 包含多个元素的响应 and/or 多个页面。

除其他外,该集合是一个 Enumerable 并将公开 Enumerable API。

有几种方法可以遍历集合的元素或抓取集合的特定元素:

workspaces = client.workspaces.find_all
workspaces.take(1)
#<Asana::Workspace id: 1234, name: "Moon Landing">

workspaces.first    
#<Asana::Workspace id: 1234, name: "Moon Landing">

workspaces.each
#<Enumerator:0x000001032df790>

还有几个examples including one using the personal access token to iterate over the workspaces available