Displaying user inputed arrays

Isaac T. wrote:

Thanks that works!

but one thing i dont really get what is going on with (gets.scan
/[\w’-]*/) - [""] ?

could you explain it? like every party i under stand the gets and i
guess the scan reads whats entered but i dont really understand the
/[\w-]*/) then i guess the - [""] is the array that what ever is
scanned is added to

actually this doesnt work the way i want it to.

when i try to retrieve and array from that i think that the scan thing
returns it as a integer other then the string i need why is this?

this is the code that im using to do this:

places = [‘first’, ‘second’, ‘third’, ‘fourth’, ‘fifth’]
puts ‘Please enter 5 words on the same line with spaces then press
enter:’

answered = false
while not answered
input_words = (gets.scan /[\w-]*/) - [""]
if input_words.length > 5
puts "Please type in FIVE words no more: "
else
answered = true
end
end

input_words.each_with_index do |word, i|
puts “To view a word you picked type:\n 1: first word\n 2: second
word\n 3: third word\n 4: fourth word\n 5: fifth word”
answered = false
while not answered
num_answer = gets.chomp
case num_answer[0]
when ‘1’[0]
i = 0
answered = true
when ‘2’[0]
i = 1
answered = true
when ‘3’[0]
i = 2
answered = true
when ‘4’[0]
i = 3
answered = true
when ‘5’[0]
i = 4
answered = true
else
puts “Try again…enter a number 1 - 5 to see one of the words
you typed”
end
end
puts “The %s word you entered was %s” % [places[i], word[i]]
end

On Wed, Feb 20, 2008 at 3:56 PM, Isaac T. [email protected]
wrote:

Isaac T. wrote:

puts "Please type in FIVE words no more: "

So, it’s okay to have less than five words?

On Wed, Feb 20, 2008 at 12:19 PM, Isaac T.
[email protected] wrote:

Im not really wanting it to take only the first 5 words i want it to say
something if it is more then 5 words

print "Please type in 5 words with spaces inbetween them: "

answered = false
while not answered
a = [gets.chomp.split]
if a.length > 5
puts “Type in FIVE words no more:”

You’re very close - it’s simply a = gets.chomp.split (without the
square brackets). I recommend splitting on " " explicitly rather than
relying on it being the default.

a = gets.chomp.split(" ")

Note that what you did was create an array containing the return value
of gets.chomp.split:

input = “hello world”
a = input.split #=> [“hello”, “world”]
a = [input.split] #=> [[“hello”, “world”]]

in the latter case, you get an array of length 1, whose only element
is an array of length 2.

martin

sorry i dont think you can edit posts…
i found out that the amount of words that you enter is the amount of
times it loops the question to pick a word

On Wed, Feb 20, 2008 at 3:56 PM, Isaac T. [email protected]
wrote:

enter:’
end
end

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]

Todd

The gets.chomp.split(" ") works for checking the amount of words but now
for my code it doesnt return the words

places = [‘first’, ‘second’, ‘third’, ‘fourth’, ‘fifth’]
puts ‘Please enter 5 words on the same line with spaces then press
enter:’

answered = false
while not answered
input_words = gets.chomp.split(" ")
if input_words.length > 5
puts "Please type in FIVE words no more: "
else
answered = true
end
end

input_words.each_with_index do |word, i|
puts “To view a word you picked type:\n 1: first word\n 2: second
word\n 3: third word\n 4: fourth word\n 5: fifth word”
answered = false
while not answered
num_answer = gets.chomp
case num_answer[0]
when ‘1’[0]
i = 0
answered = true
when ‘2’[0]
i = 1
answered = true
when ‘3’[0]
i = 2
answered = true
when ‘4’[0]
i = 3
answered = true
when ‘5’[0]
i = 4
answered = true
else
puts “Try again…enter a number 1 - 5 to see one of the words
you typed”
end
end
puts “The %s word you entered was %s” % [places[i], word[i]]
end

it just returns “The (place) word you entered was” and thats it
i think its because now that its not in the brackets that when i do
word[i] its not returning the array word[whatever]

On Wed, Feb 20, 2008 at 4:57 PM, Todd B. [email protected]
wrote:

On Wed, Feb 20, 2008 at 3:56 PM, Isaac T. [email protected] wrote:

Isaac T. wrote:

Thanks that works!

but one thing i dont really get what is going on with (gets.scan
/[\w’-]*/) - [“”] ?

scan /[\w’-]*/

…returns an array of everything that matches the regular expression
/[\w’-]*/

This returns every string that has zero or more of a word character
(\w), an apostrophe (‘), or a hyphen (-). The - [“”] part is to
subtract all empty strings from the array. I just noticed, however,
though that you can omit that last bit if you use /[\w’-]+/ instead
(the + instead of the *). That scans for one or more, instead of
zero or more characters.

Same code, but the scan changed…

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]

Todd

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
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 and also why did
you type /[\w’-]*/) when /\w+/ works the same … i think

Please unsuscribe…


¡Capacidad ilimitada de almacenamiento en tu correo!
No te preocupes más por el espacio de tu cuenta con Correo Yahoo!:

On Wed, Feb 20, 2008 at 5:36 PM, Isaac T. [email protected]
wrote:

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

On Wed, Feb 20, 2008 at 7:39 PM, Todd B. [email protected]
wrote:

puts “Please type in exactly FIVE words, thanks.”
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

Isaac T. wrote:

Thanks you all for your help i have finished my code (so far) and just
have one question…
is this enough to be ready to move on to making a small game?

If coding up little programs like this interests you, I heartily
recommend Peter C.'s Beginning_Ruby book. It’s got a lot of
fascinating examples aimed at people who are learning how to program in
Ruby.

Good luck!

Thanks you all for your help i have finished my code (so far) and just
have one question…
is this enough to be ready to move on to making a small game?

print "What is your name? "
name = gets.chomp
puts “Hello #{name}”

print "How many years old are you? "
years_old = gets.chomp.to_i
hours_old = (years_old * 365) * 24
puts “Your name is #{name} and your are #{hours_old} hours old”

answered = false #if person has answered y/n question
print “Do you want to know how many seconds old you are #{name}? (Y/N)”
while not answered #if the person hasnt answered (makes loop question if
wrong answer)
answer = gets.chomp
seconds_old = hours_old.to_i * 60 * 60

case answer.upcase[0] #makes first letter of the answer cap and and
array
when “Y”[0] #if first letter of answer is y, Y then… ([0] is
making y, Y first letter of array or answer)
puts “You are #{seconds_old} seconds old”
answered = true #leaves the while not loop
when “N”[0] #same as Y but with N
puts “Ok Then”
answered = true #leaves the while not loop
else
puts “Ummm…Yes or No?”
end
end

answered = false
puts "type in a number 1-100: "
while not answered
print_times = gets.chomp.to_i - 1
if print_times > 100
puts “Please enter a number between 1 and 100”
else
answered = true
end
end

answered = false
puts "ok now type in a word 1-10 letters long: "
while not answered
print_word = gets.chomp
if print_word.length > 10
puts “Please enter a word between 1 and 10 letters long”
else
answered = true
end
end
1.upto(print_times) do |q|
puts print_word + “\n”
end

places = [“first”, “second”, “third”, “fourth”, “fifth”]
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 “The #{places[n - 1]} word you choose was #{a[n - 1]}”

Yeah i think its interesting to learn with but i hope to learn gui and
start making applications at some point.

Isaac T. wrote:

Yeah i think its interesting to learn with but i hope to learn gui and
start making applications at some point.

sorry i forgot to ask this what is the best to program gui in ruby?

ive heard of Tk and a few others im working on a mac so i would like one
that operates on all platforms and OS’s.

The main problem is i dont really know where to start like what do i
need to do first and are there any good tutorials for coding GUI? Just
something to get started on a program that maybe creates text in a
window if a button is pressed maybe hello world?

Thanks in advance

On Thu, Feb 21, 2008 at 11:09 AM, Isaac T.
[email protected] wrote:

need to do first and are there any good tutorials for coding GUI? Just
something to get started on a program that maybe creates text in a
window if a button is pressed maybe hello world?

Thanks in advance

I haven’t played with it yet, but there’s Shoes. You can find it at

http://code.whytheluckystiff.net/shoes/

Todd