Case loop / Program quits

Hello everyone!
I'm trying to make a little grocery list to practice. My intention is to
make basically an array that you can add to, remove and display items.
My problem starts when after a person adds an item to the array for
example and the options come up again, instead of going back into the
cases the program just quits.
It kinda is like a loop where it keeps going around and around.. my
program just doesn't wanna do that. :smiley:
Could someone explain where I went wrong with my codes?

list = Array.new(0)

display_proc = Proc.new {|x| puts x}

puts "What would you like to do with your shopping list?"

def opening_options
聽聽puts
聽聽puts "--ADD"
聽聽puts "--REMOVE"
聽聽puts "--CLEAR"
聽聽puts "--DISPLAY"
聽聽puts "--EXIT"

聽聽@options = gets.chomp.downcase
end

opening_options

case @options
聽聽when "add"
聽聽聽聽puts "What item would you like to add?"
聽聽聽聽add = gets.chomp
聽聽聽聽list << add unless list.include?(add)

聽聽聽聽opening_options

聽聽when "remove"
聽聽聽聽puts "What item would you like to remove?"
聽聽聽聽list.each(&display_proc)
聽聽聽聽remove = gets.chomp
聽聽聽聽list.delete(remove)

聽聽聽聽opening_options

聽聽when "clear"
聽聽聽聽puts "Are you sure you want to delete every item from your list?"
聽聽聽聽puts "--YES/NO"
聽聽聽聽clear = gets.chomp.downcase
聽聽聽聽case clear
聽聽聽聽聽聽when "yes" || "y"
聽聽聽聽聽聽聽聽list.delete_if {true}
聽聽聽聽聽聽聽聽opening_options
聽聽聽聽聽聽when "no" || "n"
聽聽聽聽聽聽聽聽opening_options
聽聽聽聽聽聽else "ERROR! Unknown command!"
聽聽聽聽end

聽聽when "display"
聽聽聽聽list.each(&display_proc)

聽聽聽聽opening_options

聽聽when "exit"
聽聽聽聽system('quit')
聽聽else "ERROR! Unknown command!"
end

路路路

--
Posted via http://www.ruby-forum.com/.

Hello everyone!
I'm trying to make a little grocery list to practice. My intention is to
make basically an array that you can add to, remove and display items.
My problem starts when after a person adds an item to the array for
example and the options come up again, instead of going back into the
cases the program just quits.
It kinda is like a loop where it keeps going around and around.. my
program just doesn't wanna do that. :smiley:
Could someone explain where I went wrong with my codes?

list = Array.new(0)

display_proc = Proc.new {|x| puts x}

puts "What would you like to do with your shopping list?"

def opening_options
  puts
  puts "--ADD"
  puts "--REMOVE"
  puts "--CLEAR"
  puts "--DISPLAY"
  puts "--EXIT"

  @options = gets.chomp.downcase
end

You need a loop starting here and ...

opening_options

case @options
  when "add"
    puts "What item would you like to add?"
    add = gets.chomp
    list << add unless list.include?(add)

This line should go away, similarly as all other invocations inside the case:

    opening_options

  when "remove"
    puts "What item would you like to remove?"
    list.each(&display_proc)
    remove = gets.chomp
    list.delete(remove)

    opening_options

  when "clear"
    puts "Are you sure you want to delete every item from your list?"
    puts "--YES/NO"
    clear = gets.chomp.downcase
    case clear
      when "yes" || "y"
        list.delete_if {true}
        opening_options
      when "no" || "n"
        opening_options
      else "ERROR! Unknown command!"
    end

  when "display"
    list.each(&display_proc)

    opening_options

  when "exit"
    system('quit')

This won't work. You want to invoke "exit" instead of "system".

  else "ERROR! Unknown command!"
end

... ending here.

Kind regards

robert

路路路

On Thu, Nov 7, 2013 at 6:07 PM, Greg Hajdu <lists@ruby-forum.com> wrote:

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

Hello everyone!
I'm trying to make a little grocery list to practice. My intention is to
make basically an array that you can add to, remove and display items.
My problem starts when after a person adds an item to the array for
example and the options come up again, instead of going back into the
cases the program just quits.
It kinda is like a loop where it keeps going around and around.. my
program just doesn't wanna do that. :smiley:
Could someone explain where I went wrong with my codes?

list = Array.new(0)

display_proc = Proc.new {|x| puts x}

puts "What would you like to do with your shopping list?"

def opening_options
puts
puts "--ADD"
puts "--REMOVE"
puts "--CLEAR"
puts "--DISPLAY"
puts "--EXIT"

@options = gets.chomp.downcase
end

You need a loop starting here and ...

opening_options

case @options
when "add"
   puts "What item would you like to add?"
   add = gets.chomp
   list << add unless list.include?(add)

This line should go away, similarly as all other invocations inside the case:

   opening_options

when "remove"
   puts "What item would you like to remove?"
   list.each(&display_proc)
   remove = gets.chomp
   list.delete(remove)

   opening_options

when "clear"
   puts "Are you sure you want to delete every item from your list?"
   puts "--YES/NO"
   clear = gets.chomp.downcase
   case clear
     when "yes" || "y"

when "yes", "y"

路路路

On Nov 7, 2013, at 9:18, Robert Klemme <shortcutter@googlemail.com> wrote:

On Thu, Nov 7, 2013 at 6:07 PM, Greg Hajdu <lists@ruby-forum.com> wrote:

       list.delete_if {true}
       opening_options
     when "no" || "n"
       opening_options
     else "ERROR! Unknown command!"
   end

when "display"
   list.each(&display_proc)

   opening_options

when "exit"
   system('quit')

This won't work. You want to invoke "exit" instead of "system".

else "ERROR! Unknown command!"
end

... ending here.

Kind regards

robert

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/