0

私は現在、Learn Ruby the hard way チュートリアルから Ruby を学んでいます。その演習では、著者は単純なゲームに何かを追加するように私たちに求めます。ただし、次のようなことを行うことでbear_room メソッドを改善しようとしていました。

while true
print "> "
choice = gets.chomp.downcase!
if choice.include? ("taunt")
  dead("The bear looks at you then slaps your face off.")
elsif choice.include? "taunt" && !bear_moved
  puts "The bear has moved from the door. You can go through it now."
  bear_moved = true
elsif choice.include? "taunt" && bear_moved
  dead("The bear gets pissed off and chews your leg off.")
elsif choice.include? "open" && bear_moved

ただし、これを書くと:

choice = gets.chomp.downcase!

実行時に次のエラーが表示されます。

ex35.rb:44:in `bear_room': undefined method `include?' for nil:NilClass (NoMethodError)
from ex35.rb:99:in `start'
from ex35.rb:109:in `<main>'

しかし、次のようなことをすると:

choice = gets.chomp.downcase

またはこれ:

choice = gets.chomp
choice.downcase!

できます。何故ですか?あらゆる種類の助けをいただければ幸いです。また、ビットはどのように機能しwhile trueますか?それは本当に私を混乱させます。

必要な場合に備えて、プログラムの残りの部分を次に示します。読みやすくするために、前述のメソッドを「分離」したままにします。

# Creates the 'gold_room' method, so it can be called later.
def gold_room
  puts "This room is full of gold. How much do you take?"

  print "> "
  choice = gets.chomp
  # Converts the 'choice' variable to integer type.
  choice.to_i

  # Checks if 'choice' is equals to 0, OR greater or equal to 1.
  if choice == '0' || choice >= '1'
  # Saves the integer 'choice' variable in the 'how_much' variable.
    how_much = choice.to_i
  else
    dead("Man, learn to type a number.")
  end

  # Checks if the 'how_much' variable is lesser than 50, and executes the code below if so.
  if how_much < 50
    puts "Nice, you're not greedy, you win!"
    exit(0)
  elsif how_much >= 50 && how_much < 100
    puts "Mmm, ok that's enough. Get out!"
  else
    dead("You greedy bastard!")
  end
end


################### bear_room method ###################


# Creates the 'bear_room' method.
def bear_room
puts "There is a bear here."
puts "The bear has a bunch of honey."
puts "The fat bear is in front of another door."
puts "How are you going to move the bear?"
puts "1. Taunt the bear."
puts "2. Steal the bear's honey. "

# Declares the 'bear_moved' variable as a boolean, initialize it to false.
   bear_moved = false

  while true
    print "> "
    choice = gets.chomp.downcase
    if choice.include? ("taunt")
      dead("The bear looks at you then slaps your face off.")
    elsif choice.include? "taunt" && !bear_moved
      puts "The bear has moved from the door. You can go through it now."
      bear_moved = true
    elsif choice.include? "taunt" && bear_moved
      dead("The bear gets pissed off and chews your leg off.")
    elsif choice.include? "open" && bear_moved
      gold_room
    else
      puts "I got no idea what that means."
    end
  end
end

############### end of method ###############





# Defines the 'cthulhu_room' method.
def cthulhu_room
  puts "Here you see the great evil Cthulhu."
  puts "He, it, whatever stares at you and you go insane."
  puts "Do you flee for your life or eat your head?"

  print "> "
  choice = gets.chomp

  # Checks if the user's input contains the word 'flee'. If so, executes the code below.
  if choice.include? "flee"
    start
  # Checks if the user's input contains the word 'head'. If so, executes the code below instead.
  elsif choice.include? "head"
    dead("Well that was tasty!")
  else
    # Otherwise, calls the 'cthulhu_room' method again.
    cthulhu_room
  end
end

# Defines the 'dead' method. It takes one argument (why). Example: dead("Well that was tasty!")
def dead(why)
  puts why, "Nice."
  # Succesfully finish the program.
  exit(0)
end

# Defines the 'start' method, wich is where the game begins. Duh.
def start
  puts "You are in a dark room."
  puts "There is a door to your right and left."
  puts "Which one do you take?"

  print "> "
  choice = gets.chomp

  # Start the branching. It checks the user's input, and saves that on the 'choice' variable, which is used along the whole program in the other methods.
  if choice == "left"
    # Calls the 'bear_room' method.
    bear_room
  elsif choice == "right"
    # Calls the 'cthulhu_room' method.
    cthulhu_room
  else
    dead("You stumble around the room until you starve.")
  end
end

# Start the game.
start

-------------------------------------------------- -------------------------

TL;DR:choice = gets.chomp.downcase!との違いは何ですか

choice = gets.chomp
choice.downcase!

PS:コメントは演習の一部です。何らかのタイプの修正 (コメント、質問の作成方法、一般的なコードなど)がある場合は、改善できるように教えてください。長々とすみません!

-------------------------------------------------- -----------------------

4

1 に答える 1