【问题标题】:How to return to the calling method?如何返回调用方法?
【发布时间】:2016-07-29 18:21:01
【问题描述】:

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

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) 方法?

【问题讨论】:

标签: ruby methods return


【解决方案1】:

就在这里:

   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)

您也可以将此逻辑添加到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 这样的自定义错误,您需要在某处定义错误类:

class AttackNotFoundError < StandardError; end

您实际上可以使用任何错误,例如raise StandardError,但最好限制您正在救援的错误,以免意外救援不相关的错误。

【讨论】:

  • 如果你看代码,调用方法不止一种。但是循环可能会起作用
  • 你也可以使用救援。我将为此添加一个示例。
  • 这是一个天才的想法!!!我什至没有考虑救援条款,非常感谢!
  • 当然,顺便说一句,if POKEMON_ATTACKS[pokemon][attack] == nil 这样的东西通常是不必要的,你可以删除== nilif 将评估它是否为真/假
  • 我无法确定这是否是滥用异常。习惯上,您可能更愿意使用throw/catch
猜你喜欢
  • 1970-01-01
  • 2013-09-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-15
  • 1970-01-01
相关资源
最近更新 更多