0

私はハッシュの配列を持っており(または少なくともそれらはハッシュだと思います)、それぞれのIDを引き出す必要があります。ルビーにはこれを行うためのいくつかの簡単な方法があると確信しています...私はそれを理解することができません。

配列を繰り返し処理して新しい配列を作成したくありません。

[
  [
    {
      "bio": "I am a tech geek who loves starting up companies. While I was in college, I founded Squeeze My Tees",
      "business_name": "Rounded Development",
      "city": "",
      "created_at": "2012-04-22T18:07:44Z",
      "first_name": "Brian",
      "id": 1,
      "industry": "Entertainment",
      "last_name": "Weinreich",
      "lat": null
    },
    {
      "access_token": null,
      "bio": null,
      "business_name": null,
      "city": null,
      "created_at": "2012-04-23T13:56:35Z",
      "email": "test@jambo.com",
      "first_name": "asdad",
      "id": 2,
      "industry": null,
      "last_name": "ddfs",
      "lat": null,
      "linkedin_id": null,
      "linkedin_url": null,
      "lng": null,
      "position": null,
      "professional_headline": null,
      "state": null,
      "street": null,
      "updated_at": "2012-04-23T13:56:35Z"
    },
    {
      "access_token": null,
      "bio": null,
      "business_name": null,
      "city": null,
      "created_at": "2012-04-23T13:56:39Z",
      "email": "tesasdat@jambo.com",
      "first_name": "fdsd",
      "id": 3,
      "industry": null,
      "last_name": "asdgw",
      "lat": null,
      "linkedin_id": null,
      "linkedin_url": null,
      "lng": null,
      "position": null,
      "professional_headline": null,
      "state": null,
      "street": null,
      "updated_at": "2012-04-23T13:56:39Z"
    },
    {
      "access_token": null,
      "bio": null,
      "business_name": null,
      "city": null,
      "created_at": "2012-04-23T13:56:44Z",
      "email": "asdsad@jambo.com",
      "first_name": "ewtrwef",
      "id": 4,
      "industry": null,
      "last_name": "dfd",
      "lat": null,
      "linkedin_id": null,
      "linkedin_url": null,
      "lng": null,
      "position": null,
      "professional_headline": null,
      "state": null,
      "street": null,
      "updated_at": "2012-04-23T13:56:44Z"
    }
  ]
]
4

1 に答える 1

1

IDだけを引き出すには、次のようにします。

the_IDs = array_of_hashes.collect { |single_array| single_array["id"] }

明らかに、冗長性の低い変数名を使用できます。これらは説明のためだけのものです。ただし、配列をループして、ブロックが返すものをすべて収集できるという考え方です。この場合、IDが返され続け、the_IDs収集されたものの配列になります。

于 2012-04-23T15:44:37.960 に答える