构建一个递归函数来访问ruby

时间:2017-10-21 14:46:23

标签: ruby ruby-on-rails-4 recursion hash hashmap

是否有更多的红宝石方式可以动态地实现这一点,也许是递归的。目前我的播放器桌面上有一些我关心的属性,如SURNAME,GIVEN_NAME,AT_BATS,RUNS,HITS,HOME_RUNS,RBI等。如何访问播放器的哈希值并将其加载到我的动态数据库?目前我有以下工作,但是我手动取出团队并迭代" all_players"阵列。必须有一种更简单的红宝石方式。请注意,为了示例,我跳过了下面Json示例中的右括号。

seeds.rb

file = File.read("#{Rails.root}/db/1998_stats.json")
data_hash = JSON.parse(file).with_indifferent_access

#####################ALL TEAMS ###################################
  braves  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][0]['TEAM'][0]['PLAYER']
  marlins  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][0]['TEAM'][1]['PLAYER']
  expos  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][0]['TEAM'][2]['PLAYER']
  mets  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][0]['TEAM'][3]['PLAYER']
  phillies  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][0]['TEAM'][4]['PLAYER']

  cubs  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][1]['TEAM'][0]['PLAYER']
  reds  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][1]['TEAM'][1]['PLAYER']
  astros  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][1]['TEAM'][2]['PLAYER']
  brewers  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][1]['TEAM'][3]['PLAYER']
  pirates  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][1]['TEAM'][4]['PLAYER']
  cardinals  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][1]['TEAM'][5]['PLAYER']

  diamondbacks  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][2]['TEAM'][0]['PLAYER']
  rockies  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][2]['TEAM'][1]['PLAYER']
  dodgers  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][2]['TEAM'][2]['PLAYER']
  padres  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][2]['TEAM'][3]['PLAYER']
  giants  = data_hash['SEASON']['LEAGUE'][0]['DIVISION'][2]['TEAM'][4]['PLAYER']

  orioles  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][0]['TEAM'][0]['PLAYER']
  redsox  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][0]['TEAM'][1]['PLAYER']
  yankees  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][0]['TEAM'][2]['PLAYER']
  devil_rays  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][0]['TEAM'][3]['PLAYER']
  blue_jays  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][0]['TEAM'][4]['PLAYER']

  whitesox  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][1]['TEAM'][0]['PLAYER']
  royals  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][1]['TEAM'][1]['PLAYER']
  tigers  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][1]['TEAM'][2]['PLAYER']
  indians  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][1]['TEAM'][3]['PLAYER']
  twins  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][1]['TEAM'][4]['PLAYER']

  angels  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][2]['TEAM'][0]['PLAYER']
  athletics  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][2]['TEAM'][1]['PLAYER']
  mariners  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][2]['TEAM'][2]['PLAYER']
  rangers  = data_hash['SEASON']['LEAGUE'][1]['DIVISION'][2]['TEAM'][3]['PLAYER']

  all_players = [braves, marlins, expos, mets, phillies, cubs, reds, astros, brewers, pirates, cardinals,
                    diamondbacks, rockies, dodgers, padres, giants, orioles, redsox, yankees, devil_rays, blue_jays, 
                    whitesox, royals, tigers, indians, twins, angels, athletics, mariners, rangers]

  all_players.each do |players_per_team|
    players_per_team.each do |player|
      Player.create! do |new_player|
        new_player.first = player['GIVEN_NAME']
        new_player.last = player['SURNAME']
        new_player.position  =  player['POSITION']
        new_player.hits  =  player['HITS']
        new_player.bats  =  player['AT_BATS']
        new_player.doubles  =  player['DOUBLES']
        new_player.triples  =  player['TRIPLES']
        new_player.walks  =  player['WALKS']
        new_player.hbp  =  player['HIT_BY_PITCH']
        new_player.sh  =  player['SACRIFICE_HITS']
        new_player.sf  =  player['SACRIFICE_FLIES']
        new_player.avg  =  nil
        new_player.hr  =  player['HOME_RUNS']
        new_player.rbi  =  player['RBI']
        new_player.sb  =  player['STEALS']
        new_player.ops  =  player['']
        new_player.runs  =  player['']
        new_player.obp = player['']
      end
    end
  end

的Json

{
"SEASON": {
    "YEAR": "1998",
    "LEAGUE": [
        {
            "LEAGUE_NAME": "National League",
            "DIVISION": [
                {
                    "DIVISION_NAME": "East",
                    "TEAM": [
                        {
                            "TEAM_CITY": "Atlanta",
                            "TEAM_NAME": "Braves",
                            "PLAYER": [
                                {
                                    "SURNAME": "Malloy",
                                    "GIVEN_NAME": "Marty",
                                    "POSITION": "Second Base",
                                    "GAMES": "11",
                                    "GAMES_STARTED": "8",
                                    "AT_BATS": "28",
                                    "RUNS": "3",
                                    "HITS": "5",
                                    "DOUBLES": "1",
                                    "TRIPLES": "0",
                                    "HOME_RUNS": "1",
                                    "RBI": "1",
                                    "STEALS": "0",
                                    "CAUGHT_STEALING": "0",
                                    "SACRIFICE_HITS": "0",
                                    "SACRIFICE_FLIES": "0",
                                    "ERRORS": "0",
                                    "PB": "0",
                                    "WALKS": "2",
                                    "STRUCK_OUT": "2",
                                    "HIT_BY_PITCH": "0"
                                },
                            ...

1 个答案:

答案 0 :(得分:0)

此处不需要递归。

# data_hash['SEASON']['LEAGUE'][1]['DIVISION'][0]['TEAM'][0]['PLAYER']
players = (0..1).map do |league|
  (0..3).map do |division|
    (0..40).map do |team|
      data_hash['SEASON']['LEAGUE'][league]['DIVISION'][division]['TEAM'][team]['PLAYER']
    end
  end
end.flatten

代码未经测试,但应该有效。