From: Todd Benson Date: 2008-02-21T10:51:28+09:00 Subject: Re: displaying user inputed arrays On Wed, Feb 20, 2008 at 7:39 PM, Todd Benson wrote: > On Wed, Feb 20, 2008 at 5:36 PM, Isaac Toothyxdip wrote: > > Thanks that really shortens up my code and works but i have a couple > > questions > > > > > > > > > > a, n = nil > > puts "Please enter 5 words" > > until (a = (gets.scan /[\w'-]*/) - [""]).length == 5 > > puts "Please type in exactly FIVE words, thanks." > > end > > puts "You would like to print out which word number? (1-5)" > > until (1..5).include? (n = gets.chomp.to_i) > > puts "Please pick a number 1 through 5" > > end > > puts a[n - 1] > > > > i under stand almost all of it (i just dont get what the - [""] does and > > Try [1, 2, 3, 4] - [1, 4] to understand the methodology, - [""] > removes empty strings created by scan because /[\w'-]/ is not only > greedy, but _zero_or_more_, which means it will return empty strings > for characters you don't include (like spaces). > > > > i think that means that it puts it into an array I forgot to mention the #scan method is what creates an array. > but the main questions > > i have are why did you type a, n = nil that isnt needed > > You're right. It isn't necessary. I thought it might be for scope reasons. > > > > and also why did > > you type /[\w'-]*/) when /\w+/ works the same ... i think I created a character class (the square brackets) that included \w and ' and - because "you're" is a word, as is "able-bodied". Like I said, if you use the *, you will get several empty strings in the array the the #scan method gives you, which you would have to subtract, but you can bypass that ugliness with using the + instead. hope that makes some sense, Todd