0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rubyで学んだあとに、簡単なTodoアプリを作りました!

Last updated at Posted at 2025-02-13

実際のコードはこちら

class Todo
  def initialize
    @tasks = []
   menu
  end
  def menu
    loop do
      puts "----------------"
      puts "選択してください"
      puts "1:タスクの追加" 
      puts "2:タスクの表示"
      puts "3:タスクの消去"
      puts "4:終了"
      puts "----------------"

      @input = gets.chomp.to_i
      case @input
      when 1
        add
      when 2
        display
      when 3
        erase
      when 4
        puts "終了します"
        break
      end
    end
  end

  def add
    puts "タスクを入力してください"
    @task = gets.chomp
    @tasks << @task
  end

  def display
    if @tasks.empty?
      puts "タスクはありません"
    else
    @tasks.each_with_index do |task , index|
      puts "#{index + 1} : #{task}" 
    end
  end

  def erase
    display
    puts "どれを消去しますか?"
    @del = gets.chomp.to_i - 1
    @tasks.delete_at(@del)
    puts "タスクが消去されました"

  end
end

todo = Todo.new

end

なんとか検討しながら頑張りました。
配列を入れるためには「<<」が必要だと知った。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?