Beginner 'How-to' Question on getting Array info

N

Nouman Saleem

Hi,

I am trying to learn ruby and had a question (multiple in fact, however,
i'll ask this one)

Im making a tiny script that asks for the names of a person's children,
then replies by saying hi to each child.

for example:

what's the names of these precious babies?
user types: John, Greg, Sam
replies: Hi John, Hi Greg, Hi Sam.

so far I was working with a single person, but I understood that part:
def welcome(name)
result = "Hey, " + name
end
puts "hey what's your name?"
name = gets
puts welcome(name)

any help would be great ;] hope it's as easy as it sounds!
 
M

Michael Malone

Nouman said:
Hi,

I am trying to learn ruby and had a question (multiple in fact, however,
i'll ask this one)

Im making a tiny script that asks for the names of a person's children,
then replies by saying hi to each child.

for example:

what's the names of these precious babies?
user types: John, Greg, Sam
replies: Hi John, Hi Greg, Hi Sam.

so far I was working with a single person, but I understood that part:
def welcome(name)
result = "Hey, " + name
end
puts "hey what's your name?"
name = gets
puts welcome(name)

any help would be great ;] hope it's as easy as it sounds!

def welcome(name)
result = "Hey, " + name
end

str = 'init'
children = []
while !str.empty?
puts "Hey, what's your name?"
str = gets.chomp
children << str unless str.empty?
end

children.each { |child| puts welcome(child) }

That will do it. Hit enter without any chars to finish typing names....

=======================================================================
This email, including any attachments, is only for the intended
addressee. It is subject to copyright, is confidential and may be
the subject of legal or other privilege, none of which is waived or
lost by reason of this transmission.
If the receiver is not the intended addressee, please accept our
apologies, notify us by return, delete all copies and perform no
other act on the email.
Unfortunately, we cannot warrant that the email has not been
altered or corrupted during transmission.
=======================================================================
 
N

Nouman Saleem

thanks for taking the time to help Michael, this works great. Now time
to break it down and see how you did it :)

thanks again!
 
T

Todd Benson

thanks for taking the time to help Michael, this works great. Now time
to break it down and see how you did it :)

thanks again!

Here's one for you that uses some other methods...

puts "What are the kids' names?"
puts( gets.chomp.split.map {|n| "Hi #{n}"}.join(", ") )

... basically, #split method builds array, #map changes prepends each
element with "Hi ", and #join builds a string separating each element
with ", ".

This assumes the user doesn't use commas to separate names. If they
do, you can split on commas with split(","), or use regular
expressions.

Todd
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top