0

I am trying to create a program in ruby where in users will be able to continually add an item and it will continually show up the contents of the array until the word "EXIT" was submitted.

I also trying to figure it out how can I add a program where in I can remove the specific item name using array.pop[] on the list but the program still works.

Here's what I got so far:

array = []
puts "WELCOME TO THIS PROGRAM"
puts "You can add any item as much as you want. Type EXIT if you want to STOP!"
print "Add a new item: "
user_input = gets.chomp
array.push user_input
    puts "HERE ARE THE CONTENT OF THE ARRAY "
    p(array)

while user_input != "EXIT"
    puts "Add a new item: "
    user_input = gets.chomp
    array.push user_input
    puts "HERE ARE THE CONTENT OF THE ARRAY "
    p(array)
end

Any idea what my program is lacking?

2
  • I don't quite understand the question. Are you asking how to improve the program in terms for legibility/maintainability? In this case, I suggest replacing lines 4 to 8 of your program by the single line user_input = nil. Commented Aug 9, 2016 at 4:19
  • There's some obvious duplication here which is usually a sign the structure isn't quite right. In particular, the input fetch part. Commented Aug 9, 2016 at 5:11

2 Answers 2

2

The simplest working version of this code involves restructuring it ever so slightly. Using case is great for breaking out special behaviour like you have here with the "EXIT" trigger:

array = []

puts "WELCOME TO THIS PROGRAM"
puts "You can add any item as much as you want. Type EXIT if you want to STOP!"

loop do
  print "Add a new item: "

  case (input = gets.chomp)
  when 'EXIT'
    break
  else
    array << input
  end
end

puts "HERE ARE THE CONTENT OF THE ARRAY "
p(array)

It's worth noting that this idea of printing and pestering for input is really irregular in most Ruby programs, it's very limiting. The $stdin file-handle is best used for this, and there's no need for an explicit "EXIT" call, file-handles will return EOF when there's no more data. You can trigger these manually with CTRL+D on most systems.

That makes the program much more minimal:

# Pull in all lines from $stdin and run chomp on each.
array = $stdin.readlines.map(&:chomp)

puts "HERE ARE THE CONTENT OF THE ARRAY "
p(array)

It also means you can do things like this:

ruby my_program.rb < inputfile.txt

With your syntax you'd have to manually add "EXIT" to the end of any file you wanted to process. This is very anti-UNIX which likes to make things easy to read and write from, such that you can chain them together.

If you want to remove items from a list, you can do that with Array#delete. When learning Ruby be sure to have the documentation open for any class you're working with. There's usually a tool that does the job you want, or two tools that can be used in conjunction.

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

1 Comment

Nice , all in a oneliner
0

It is best to bundle your logic into methods and choose a naming convention that is relevant to what the method is intended to do"self-explanatory". Your code seems to work fine except that you will need to bundle it into a method and create another method for popping elements from an array"deleting elements ".

I sketched something real quick and it works fine ,but (of course) you still need to tune it up and adding the rest of the logic for the overall control of your program / app / ruby class etc.:

   #!/usr/bin/env ruby

def run_me 

    puts "WELCOME TO THIS PROGRAM"

    # add items to an array.  
    add_to_ary 

    # test data for removing items from an array . 
    ary_of_items = ["item1", "item2", "item3", "item4", "item5"]
    remove_from_ary(ary_of_items)

end 


def add_to_ary 

    ary = []   
    puts "You can add any item as much as you want. Type EXIT if you want to STOP!"
    print "Add a new item: "
    user_input = gets.chomp
    ary.push user_input
    puts "HERE ARE THE CONTENT OF THE ARRAY "
    p(ary)

  while user_input != "EXIT"
    puts "Add a new item: "
    user_input = gets.chomp
    ary.push user_input
    puts "HERE ARE THE CONTENT OF THE ARRAY "
    p(ary)
  end

# return the array so that we can pass as an argument when deleting
  return ary   
end


def remove_from_ary(ary)

    puts "eneter the position of the element that you want to delete:"
    pos  = gets.chomp
    p ary.delete_at(pos.to_i)
    puts "deleted and element..."
    p(ary)

  while pos != "EXIT"
    puts "delete another element: "
    pos  = gets.chomp
    ary.delete_at(pos.to_i)
    puts "deleted another element... "
    p(ary)

  end

end


#run main here..
run_me

Finally, do NOT name your variables/arrays/hashes with things like "array", "hash", "system" , "myvariabl" etc. Some of these are reserved and will lead to issues, plus it is confusing.

6 Comments

I'd recommend using standard indentation for your methods and blocks. Currently your code looks very Pythonish, which isn't how Ruby is written.
Thanks @theTinMan .. You are right about that , will do.
Defining a main method in Ruby is highly irregular and like @theTinMan says, it's probably an accent you've picked up from another language.
That's fine, but keep in mind that naming a method main in Ruby implies a special meaning, or that it might be run automatically, neither of which are true. In Ruby, often unlike Java, less is more. It's a cultural thing and the When in Rome principle applies.
"the When in Rome principle applies". Most definitely. Working in a Ruby team and writing non-idiomatic code is a good first step toward being singled out in what will become an uncomfortable code-review meeting.
|

Your Answer

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