You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ruby中如何限制用户未执行文件读取函数时无法调用其他菜单功能

Solution: Track File Load State with Instance Variables

Got it, let's tackle this problem together! The core issue here is that you need a way to remember whether the user has successfully loaded the album file (via option 1) before allowing them to use options 2, 3, or 4. Instead of trying to reuse the return value of read_in_albums (which caused unwanted repeats of the function), we'll use a state variable to track the file's loaded status.

Step 1: Add a State Variable to Track Load Status

We'll use an instance variable (like @albums_loaded) to keep track of whether the file has been successfully read. We'll also store the file's content in another instance variable (@album_data) so subsequent functions can access it without re-opening the file.

Step 2: Update read_in_albums to Set the State

Modify your read_in_albums function to set the state variable when the file is successfully loaded:

def read_in_albums
  puts "Enter file name: "
  begin
    file_name = gets().chomp
    if file_name == "albums.txt"
      puts "File is open"
      # Read the entire file content and store it
      @album_data = File.readlines(file_name)
      # Mark the file as loaded
      @albums_loaded = true
      finished = true
    else
      puts "Please re-enter file name: "
    end
  end until finished
end

Step 3: Check the State in the Main Menu

In your main function, initialize the state variable first. Then, for options 2, 3, and 4, check if @albums_loaded is true before running their respective functions. If not, show the "no file selected" message:

def main()
  finished = false
  # Initialize the state variable to false (file not loaded yet)
  @albums_loaded = false
  begin
    puts("Main Menu:")
    puts("1- Read in Album")
    puts("2- Display Album Info")
    puts("3- Play Album")
    puts("4- Update Album")
    puts("5- Exit")
    choice = read_integer_in_range("Please enter your choice:", 1, 5)
    case choice
    when 1
      read_in_albums
    when 2
      if @albums_loaded
        display_album_info
      else
        puts "no file selected"
      end
    when 3
      if @albums_loaded
        play_album
      else
        puts "no file selected"
      end
    when 4
      if @albums_loaded
        update_album
      else
        puts "no file selected"
      end
    when 5
      finished = true
    end
  end until finished
end

# Helper functions (you can fill in the logic as needed)
def display_album_info
  puts "Displaying album information:"
  @album_data.each { |line| puts line.chomp }
end

def play_album
  puts "Playing the album..."
  # Add your play logic here using @album_data
end

def update_album
  puts "Updating album details..."
  # Add your update logic here using @album_data
end

def read_integer_in_range(prompt, min, max)
  # Ensure the user enters a valid integer in the specified range
  loop do
    puts prompt
    input = gets.chomp
    if input.match?(/^\d+$/)
      num = input.to_i
      return num if num.between?(min, max)
    end
    puts "Invalid input! Please enter a number between #{min} and #{max}."
  end
end

main()

Why This Works

  • No repeated function calls: The read_in_albums function only runs when the user explicitly selects option 1.
  • Clear state tracking: The @albums_loaded variable acts as a flag to check if the file is ready for use.
  • Reusable file content: Storing @album_data means you don't have to re-read the file every time you need to access its contents.

内容的提问来源于stack exchange,提问作者Theo1290

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 19:17:30