From: Todd Benson Date: 2008-02-21T10:39:31+09:00 Subject: Re: displaying user inputed arrays 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 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 Yep, you're right accept you won't need the - [""] with /[\w'-]+/ because you won't get empty strings in the array with _one_or_more_ of those characters (the + instead of the *). I sort of tried to get that across with my last post. I'm not saying the last code I posted (with +, not *, and without the - [""]) is the best, but I like it because it sort of makes grammatical sense. cheers, Todd