Dunaevlad
@Dunaevlad

Как получить значения только определенных ключей в hash?

data = {
  "player" => {        
      "PlayerId" => "16919853080087905990201061748435842063573506113420581758304706335291498269520",
      "FullName" => "Robert Lewandowski",
      "DateOfBirth" => "21/8/1988",
      "TeamId" => "bayern-munchen-munchen",
      "Age" => 33,
      "Slug" => "robert-lewandowski",
      "DisplayName" => "Robert Lewandowski",
      "Position" => "Forward",
      "PictureUrl" => "https://assets.sorare.com/seasonpicture/d30ad14a-0c0a-4ca1-a88a-87c0e2096037/picture/b41df4f95b7d1e9001b33be0248a9f19.png",
      "TrimmedPictureUrl" => "https://assets.sorare.com/seasonpicture/d30ad14a-0c0a-4ca1-a88a-87c0e2096037/picture/trimmed-765ac725f9df44e1c5dc435cf7db73cc.png",
      "Height" => 184,
      "Weight" => 80,
      "BirthDate" => "1988-08-21T00:00:00Z",
      "Avatar" => "https://assets.sorare.com/seasonpicture/d30ad14a-0c0a-4ca1-a88a-87c0e2096037/picture/avatar-3d48e1f60ca1bae3c4924e45240e0ab6.png",
      "Licensed" => false,
      "SubscriptionsCount" => 138918,
      "PlayingStatus" => "starter",
      "SquaredPictureUrl" => "https://assets.sorare.com/seasonpicture/d30ad14a-0c0a-4ca1-a88a-87c0e2096037/picture/squared-15184c2a83b47881814394e99aaeaf3e.png",
      "NationalTeam" => "poland",
      "MatchName" => "R. Lewandowski",
      "JerseyNumber" => 9
    },

    "team" => {
  
      "TeamId" => "bayern-munchen-munchen",
      "DisplayName"=> "FC Bayern München",
      "League"=> "Bundesliga",
      "Licensed"=> false,
      "ClubId"=> "null",
      "Code"=> "FCB",
      "City"=> "null",
      "Country"=> "null",
      "TeamName"=> "null",
      "PictureUrl"=> "https://assets.sorare.com/club/157dab87-1c6c-4db3-942e-f5468b65ac91/picture/e00fbb7fe56b81c82da15f8425f0df04.png",
      "LeagueSlug"=> "bundesliga-de",
      "EligiblePlayers"=> 26,
      "International"=> false,
      "ShortName"=> "Bayern München",
      "ClubName"=> "Bayern München"
    },
}
puts data['player']['Age']

Как c помощью each сразу получить значения:
['player']=>['Age'], 
['player']=>['Position'], 
['team']=>['LeagueSlug'],
  • Вопрос задан
  • 49 просмотров
Решения вопроса 1
Freika
@Freika
Senior Ruby on Rails developer
Можно и без `.each`:

{
  'Age' => data.dig('player', 'Age'),
  'Position' => data.dig('player', 'Position'),
  'League' => data.dig('team', 'LeagueSlug'),
}


Что-то вроде этого
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы