0

メソッドから何かを返したいときはいつでも、メソッドをメソッドに渡すのが本当に苦手です。どうすればそれを渡すことができるか説明してもらえますか。これが私のハッシュです

$choosen_gun = {}
$Weapon = {
    :Bazoka => ["Bazoka",5],
    :Machine_gun => ["Machine_gun",1000],
    :Hand_gun => ["Hand_gun",24,2],
    :Double_Hand_gun => ["Double_Hand_gun",24,4],
    :Sniper => ["Sniper",12,1],
    :Shot_gun => ["Shot_gun",8,2]
}

メソッド Weapon のコードは次のとおりです

    def Weapon
        puts "Now it's time to select your weapon."
        puts "Please choose a weapon that is good throughout the game."
        puts "Whenever you are shortage of bullets, please reload it."
        puts "Please avoid last minute of reloading of weapon."
        puts "Now choose your weapon based on your own preferences."
        print  "\n"

        puts "Type 1"
        puts "Gun Name: Bazoka"
        puts "Description: A powerful gun that is strong with only 5 bullets."
        puts "Rating: ★ ★ ★ ★"
        num = gets.chomp.to_i

       case num 
          when 1 
          puts "Selection of Bazoka is chosen"
          puts "Loaded 5 bullets only"
          $choosen_gun[num] = $Weapon[:Bazoka]
       end      
     return num
end

メソッドの呼び出し時。ユーザーは自分の武器を選択し、それを $choosen_gun ハッシュに num で追加し、ユーザーが入力した num を返します。

メソッド ZombieRoom のコードは次のとおりです

    def ZombieRoom(w)
    zombie = {
        :Construcied => [5],
        :Invader => [5],
        :Damned => [5],
        :Steampunk => [5],
        :Stoner => [5],
        :Wasted => [5],
        :Romero => [5]
    }
             puts "Welcome to the worst night mare of Zombie Room"
             puts "You will be fighting with a random zombie"


             while true 
             puts ".........."
             puts "Selecting a random zombie"
             puts "Selecting your prefered gun...."
             case w 
                   when 1 
                   $choosen_gun[1]
                   puts "Your selected gun is #{$choosen_gun[1][0]}"
                   #values = zombie.values
                   #puts values[rand(values.size)]
                   #random_zombie = zombie.keys.sample(1)
                   #puts random_zombie[   
                    random_zombie = zombie.to_a.sample(1).to_h
                    random_zombie.each do |key,value|
                    puts "Your random zombie is #{key}"
                    puts "With a health value of #{value[0]}"


                    puts "Time to take down that zombie now."
                    while true
                    puts "Type Shoot to knock it down or quit."
                    choice = gets.chomp
                    if $choosen_gun[1][1] >= 1
                        health = value[0] -= 1
                        $choosen_gun[1][1] -= 1 
                    puts "#{key} health now is #{health}"
                    else
                    puts "Please reload your gun"
                    puts "Reloading......"
                    $choosen_gun[1][1] += 5  
                    end 

                    if health == 0 
                        puts "You have defeated #{key}"
                        puts "Congrats!!!"
                        puts "We are happy for you"
                        puts "Lets begins to collect your prize"
                         CollectPrize()
                     else
                        puts "You did not defeat the #{key} yet"
                    end

                    end

                    end
       end
     end
   end

メソッド CollectPrize のコードは次のとおりです

def CollectPrize
      puts "Congratulations on defeating"
      puts "We would now like to give you some case prizes"

      print "\n"

      puts "Please choose only 1 prize for yourself"
      print "\n"
      puts "Type 1"
      puts "$50,000"
      print "\n"
      puts "Type 2"
      puts "$25,000"
      print "\n"
      puts "Type 3"
      puts "$55,000"
      hoho = gets.chomp.to_f


      if hoho == 1
            puts "hehe"
      end
end

ここでメソッドを呼び出す方法

ZombieRoom(Weapon())
CollectPrize()

ここでの問題は、CollectPrize メソッドが呼び出され、入力を入力して賞品の例 1 を収集するたびに、「$50,000」と出力されることです。問題を終わらせる代わりに、ZombieRoom に戻り、「タイプ シュートして倒すか終了するか」でループを続けます。少なくとも誰かがこの問題を解決する適切な方法、またはメソッドを渡す他の方法を教えてもらえますか?

4

2 に答える 2

1

ruby の定数は大文字で始まります。メソッドは常に小文字で定義されます。

irbでこれを試してください

irb(main):001:0> def Weapon
irb(main):002:1> end
=> :Weapon
irb(main):003:0> Weapon
NameError: uninitialized constant Weapon

ruby の命名規則を使用して問題の名前のメソッドを解決するには:
zombie_roomなどcollect_prize

次に、このコードが機能します。
zombie_room(weapon())

そこで行っていることは、メソッド 武器をメソッド ゾンビ ルームに渡すことではありません。実際に起こっていることは、メソッド Weapon が実行され、値が返され、その値の結果がメソッドzombie_room に渡されるということです。

それがあなたが望んでいたことだと思います。

メソッドを渡す必要がある場合は、ドキュメントを確認するproclambda、単にブロックを使用してください。

于 2017-01-11T14:01:22.047 に答える
1

あなたのコードは大きなwhile trueループにあります。true は常に true であるため、終了することはありません。そのため、CollectPrize() を呼び出した後はステートメントに戻るだけwhileです。

breakの後に行を挿入することで抜け出すことができますが、この行の周りにCollectPrize()別のwhile trueループがあります。

while ループを終了する方法に注意を払う必要があると思います。

puts "Time to take down that zombie now."
while true # <---------------- this is ALWAYS going to loop, without end
  puts "Type Shoot to knock it down or quit."
  choice = gets.chomp
  if $choosen_gun[1][1] >= 1
    health = value[0] -= 1
    $choosen_gun[1][1] -= 1 
    puts "#{key} health now is #{health}"
  else
    puts "Please reload your gun"
    puts "Reloading......"
    $choosen_gun[1][1] += 5  
  end 
  if health == 0 
    puts "You have defeated #{key}"
    puts "Congrats!!!"
    puts "We are happy for you"
    puts "Lets begins to collect your prize"
    CollectPrize()
  else
    puts "You did not defeat the #{key} yet"
  end
end
于 2017-01-11T14:40:11.430 に答える