Guest User

Untitled

a guest
Aug 20th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. arranging the output in a different line
  2. command = ""
  3. names = []
  4. while command != "exit"
  5. puts 'please enter names seperated by a space: '
  6.  
  7. command = gets.chomp!
  8.  
  9. if command == "quit"
  10. names.sort! do|a,b| a.upcase <=> b.upcase end # use {...} for doend, in a single entry rather then multiple
  11. names.each_with_index do |name, index|
  12. if index % 2 == 0
  13. puts "#{name}", "n"
  14. else
  15. puts "#{name.reverse}", "n"
  16. end
  17. end
  18. else
  19. names << command
  20. end
  21. end
  22.  
  23. names = []
  24. while true
  25. puts 'please enter names seperated by a space: '
  26. command = gets.chomp
  27. break if command == 'exit' #exit loop if exit is entered
  28. command.split(' ').each {|a| names.push(a)} # split up each word with space as delimiter and insert into the array
  29. end
  30.  
  31.  
  32. temp = []
  33. names.each do |each| #cycle thru array
  34. puts each # print each entry
  35. temp.push(each) if names.index(each).odd? #if entry is odd then add to temp array
  36. end
  37. puts '======'
  38. temp.each {|each| puts each.reverse} #print each entry reversed
Add Comment
Please, Sign In to add comment