如何返回调用方法?

时间:2016-07-29 18:21:02

标签: ruby methods return

我有一个使用验证方法的程序,如果验证失败,我想返回调用它的方法,例如:

def obtain_pokemon_name
  print 'Enter Pokemon: '
  pokemon = gets.chomp.capitalize
  obtain_basic_attack(pokemon)
end

def obtain_basic_attack(poke)
  print 'Enter basic attack: '
  basic_attack = gets.chomp.downcase
  check_attacks(poke, basic_attack)
  obtain_spec_attack(poke)
end

def obtain_spec_attack(poke)
  print 'Enter special attack: '
  spec_attack = gets.chomp.downcase
  check_attacks(poke, spec_attack)
end

def check_attacks(pokemon, attack)
  if POKEMON_ATTACKS[pokemon][attack] == nil
    puts "#{attack} is not one of #{pokemon}'s attacks, try again.."
    return # to where this function was called
  else
    attack
  end
end

begin
  obtain_pokemon_name
rescue => e
  puts "Failed with error code: #{e}"
end

运行时:

Enter Pokemon: arbok
Enter basic attack: eat
eat is not one of Arbok's attacks, try again..
Enter special attack: test
test is not one of Arbok's attacks, try again..

攻击清单:

POKEMON_ATTACKS = {
    'Bulbasaur' => {'tackle' => 10.9, 'vine whip' => 15.4, 'power whip' => 21.4, 'seed bomb' => 12.5, 'sludge bomb' => 19.2},
    'Ivysaur' => {'razor leaf' => 10.3, 'vine whip' => 15.4, 'power whip' => 21.4, 'sludge bomb' => 19.2, 'solar beam' => 13.3},
    'Kakuna' => {'bug bite' => 13.3, 'poison sting' => 10.3, 'struggle' => 8.8},
    'Beedrill' => {'bug bite' => 13.3, 'poison jab' => 14.3, 'aerial ace' => 8.6, 'sludge bomb' => 19.2, 'x-scissor' => 14.3},
    'Pidgey' => {'quick attack' => 7.5, 'tackle' => 10.9, 'aerial ace' => 8.6, 'air cutter' => 7.6, 'twister' => 5.6},
    'Ekans' => {'acid' => 9.5, 'poison sting' => 10.3, 'gunk shot' => 20.0, 'sludge bomb' => 19.2, 'wrap' => 3.8},
    'Arbok' => {'acid' => 9.5, 'bite' => 12.0, 'dark pulse' => 12.9, 'gunk shot' => 20.0, 'sludge wave' => 17.6},
}

所以我的问题是,如果数据中没有攻击,我该如何返回调用方法?例如,如果我调用arbok并且他的攻击是tackle,如果它不存在于哈希中,我将如何返回obtain_basic_attack(poke)方法?

1 个答案:

答案 0 :(得分:2)

在这里:

block

你应该再次调用原始方法。即。

flex

您也可以将此逻辑添加到 puts "#{attack} is not one of #{pokemon}'s attacks, try again.." return # to where this function was called

  if POKEMON_ATTACKS[pokemon][attack] == nil
    puts "#{attack} is not one of #{pokemon}'s attacks, try again.."
    return obtain_spec_attack(poke)

修改

再次查看你的问题,我意识到你想要返回多个级别的方法。您可以使用我已经概述的方法,或者使用rescue:

obtain_spec_attack

要使用def obtain_spec_attack(poke) loop do print 'Enter special attack: ' spec_attack = gets.chomp.downcase attack_found = check_attacks(poke, spec_attack) if attack_found break attack_found # this will return attack_found from the loop else puts "attack not found" end end end 之类的自定义错误,您需要在某处定义错误类:

def obtain_basic_attack(poke)
  begin
    print 'Enter basic attack: '
    basic_attack = gets.chomp.downcase
    check_attacks(poke, basic_attack)
    obtain_spec_attack(poke)
  rescue AttackNotFoundError
    retry # runs the 'begin' block again
  end
end

def obtain_spec_attack(poke)
  print 'Enter special attack: '
  spec_attack = gets.chomp.downcase
  check_attacks(poke, spec_attack)
end

def check_attacks(pokemon, attack)
  if POKEMON_ATTACKS[pokemon][attack] == nil
    puts "#{attack} is not one of #{pokemon}'s attacks, try again.."
    raise AttackNotFoundError
  else
    attack
  end
end

您可以使用任何错误,例如AttackNotFoundError,但最好限制您正在营救的错误,这样您就不会意外地挽救无关的错误。