0

Why does the program quit after being executed in the command line?

I saved the code below as a .rb file. When I run it, it goes through everything but it will not show me the resulting hash that I want to view. Instead, the program quits.

    def create_list
    print "What is the list name? "
    name=gets.chomp

    hash={"name"=>name,"items"=>Array.new}
    return hash
    end

    def add_list_item
    print "What is the item called? "
    item_name=gets.chomp

    print "How much? "
    quantity=gets.chomp.to_i

    hash={"name"=>item_name, "quantity"=>quantity}
    return hash
    end


   def print_separator(character="-")
    puts character *80

    end


   def print_list(list)
     puts "List: #{list['name']}"
     print_separator()

   list["items"].each do |item|
   puts "\tItem: " + item['name'] + "\t\t\t" +
   "Quantity: " + item['quantity'].to_s

   end
    print_separator()

    end

   list=create_list()
   list['items'].push(add_list_item())
   list['items'].push(add_list_item())

   puts "Here is your list: \n"
   print_list(list)
4
  • Where? Please let me know where it is and whether that is causing my problem Commented Aug 29, 2017 at 23:26
  • Added this at the end and the program did not exit and I was able to see the list: puts "Press RETURN when you're done." gets Commented Aug 29, 2017 at 23:52
  • 3
    This indentation is pure anarchy. If you organized your code better and got things properly indented the mistakes would be far more obvious. Commented Aug 30, 2017 at 0:21
  • 1
    Are you running this script by double clicking on it in Explorer? If so, it's creating a new console. When the script exits the console has no attached processes and also exits. If you run the script from CMD, it should inherit CMD's console instead of creating a new one. Then when the script exits CMD will resume as the console foreground process. Commented Aug 30, 2017 at 0:33

1 Answer 1

1

I took a look at your code, i recommend when ever you face problems of this kind to run the command ruby -wc file_name.rb , this is what it printed out:

list.rb:22: warning: *' after local variable or literal is interpreted as binary operator

list.rb:22: warning: even though it seems like argument prefix

list.rb:24: warning: mismatched indentations at 'end' with 'def' at 21

list.rb:38: warning: mismatched indentations at 'end' with 'def' at 27

Syntax OK

So after fixing the indentations the next thing you have to fix is the method print_separator:

def print_separator(character="-")
     puts character *80
end

Change it to:

def print_separator()
    80.times do |n|
      print "-"
    end
    puts
end

Here also is a working version of the same code:

def create_list
  print "What is the list name? "
  name=gets.chomp

  hash={"name"=>name,"items"=>Array.new}
  return hash
end

def add_list_item
  print "What is the item called? "
  item_name=gets.chomp

  print "How much? "
  quantity=gets.chomp.to_i

  hash={"name"=>item_name, "quantity"=>quantity}
  return hash
end


def print_separator()
    80.times do |n|
       print "-"
    end
    puts
end


def print_list(list)
  puts "List: #{list['name']}"
  print_separator()

  list["items"].each do |item|
    puts "\tItem: " + item['name'] + "\t\t\t" +
    "Quantity: " + item['quantity'].to_s
  end
  print_separator()
end

list=create_list()
list['items'].push(add_list_item())
list['items'].push(add_list_item())

puts "Here is your list: \n"
print_list(list)

Output:

What is the list name? My list
What is the item called? apple
How much? 2
What is the item called? orange
How much? 2
Here is your list:
List: My list --------------------------------------------------------------------------------
Item: apple Quantity: 2
Item: orange Quantity: 2
--------------------------------------------------------------------------------

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.