From: Robert Klemme Date: 2013-03-30T07:28:44+09:00 Subject: Re: Nil or enter with no input --047d7bea35181e977504d917cb8d Content-Type: text/plain; charset=ISO-8859-1 On Fri, Mar 29, 2013 at 5:42 PM, Joel Pearson wrote: > You might want to look into String#empty? > That alone is not sufficient. You need to at least throw String#chomp in the mix: array = [] word = nil do puts 'Enter a word' word = gets.chomp # cut off line endings array.push word unless word.empty? end until word.empty? puts array.sort Another approach would verify that the string does indeed contain a word: ... do puts 'Enter a word' word = gets.chomp # cut off line endings array.push word unless /\A\w+\z/ =~ word end until word.empty? We can also avoid the duplicate check: array = [] word = nil loop do puts 'Enter a word' word = gets.chomp # cut off line endings break if word.empty? array.push word end puts array.sort Kind regards robert -- remember.guy do |as, often| as.you_can - without end http://blog.rubybestpractices.com/ --047d7bea35181e977504d917cb8d Content-Type: text/html; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable



On Fri, Mar 29, 2013 at 5:42 PM, Joel Pearson <= ;lists@ruby-forum= .com> wrote:
You might want to look into String#empty?

That alone is not sufficient. =A0You need to at least throw Strin= g#chomp in the mix:

a= rray =3D []
word =3D nil

do
=A0 puts 'E= nter a word'
=A0 word = =3D gets.chomp # cut off line endings
=A0 array.push = word unless word.empty?
en= d until word.empty?

puts array.sort

Another approach would verify that the str= ing does indeed contain a word:

...
do
=A0 puts 'Enter a word'
=A0 word =3D ge= ts.chomp # cut off line endings
=A0 array.push word unless /\A\w+\z/= =3D~ word
end until word.empty?

We can also av= oid the duplicate check:

array =3D []
word =3D nil

loop do
=A0 puts 'Enter a word'
=A0 word =3D ge= ts.chomp # cut off line endings
=A0 break if word.empty?
=A0 array.push word
end

puts array.sort

Kind regards

robert

--
remember.guy do |as, o= ften| as.you_can - without end
http://blog.rubybestpractices.com/
--047d7bea35181e977504d917cb8d--