-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwordrepeat.rb
More file actions
22 lines (22 loc) · 994 Bytes
/
wordrepeat.rb
File metadata and controls
22 lines (22 loc) · 994 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
puts 'Give me some words and i will sort them for you and remove all duplicates.'
puts
words = []
while true
puts 'Type in word please. When you\'re done, press Enter on an empty row.'
word = gets.chomp
if word == ''
break
end
words.push word
end
puts 'Here are your words sorted by name and with all duplicates removed using the .uniq method.'
puts words.uniq.sort
puts
puts 'And here are all the words you typed in from the beginning'
puts words #all words in the words array still remain since i used the .uniq method which returns a NEW array with all duplicates removed.
puts
puts 'Here are your words sorted by name and with all duplicates removed using the .uniq! method instead!'
puts words.uniq! #Duplicates will now be removed from the words array since .uniq! ALTERS the existing array instead of creating a new one.
puts
puts 'And now the duplicates in the array words are gone since the .uniq! method ALTERS the existing array and removes all duplicates.'
puts words