Console IO

S

semmons99

Is there a simpler way to do the following?

<code>

Person = Struct.new( :first_name, :last_name )

print "Enter first name: "
first_name = gets.chomp.capitalize

print "Enter last name: "
last_name = gets.chomp.capitalize

p = Person.new( first_name, last_name )

printf "Hello %s %s.\n", p.first_name, p.last_name

</code>

My idea was to create a function the prints a message and then takes
console output. What I want to do is not have to create the extra
objects for first and last name. Below is the solution I can up with.

<code>

def pgets( msg )
print msg
gets
end

Person = Struct.new( :first_name, :last_name )

p = Person.new(
pgets( "Enter first name: " ).chomp.capitalize,
pgets( "Enter last name: " ).chomp.capitalize
)

printf "Hello %s %s.\n", p.first_name, p.last_name

</code>

Thanks,
Shane
 
J

James Edward Gray II

Is there a simpler way to do the following?

HighLine makes this kind of thing pretty simple:

Neo:~/Desktop$ ls
person.rb
Neo:~/Desktop$ ruby -rubygems person.rb
Name? (last, first) Gray, James
--- !ruby/struct:person
first_name: James
last_name: Gray
Neo:~/Desktop$ cat person.rb
#!/usr/local/bin/ruby -w

require "highline/import"
require "yaml"

class Person < Struct.new:)first_name, :last_name)
def self.parse( input )
if input =~ /^\s*(\w+),\s*(\w+)\s*$/
self.new($2, $1)
else
raise ArgumentError, "Invalid name format."
end
end
end

who = ask("Name? (last, first) ", Person)
y who

__END__

Hope that helps.

James Edward Gray II
 
D

David Ishmael

Not to shanghai this convo (but its sort of on the same subject), is there a
way to prompt for a response in-line with your question rather than printing
the question and then having it go to the next line?

-Dave


-----Original Message-----
From: James Edward Gray II [mailto:[email protected]]
Sent: Friday, March 24, 2006 12:13 PM
To: ruby-talk ML
Subject: Re: Console IO

Is there a simpler way to do the following?

HighLine makes this kind of thing pretty simple:

Neo:~/Desktop$ ls
person.rb
Neo:~/Desktop$ ruby -rubygems person.rb
Name? (last, first) Gray, James
--- !ruby/struct:person
first_name: James
last_name: Gray
Neo:~/Desktop$ cat person.rb
#!/usr/local/bin/ruby -w

require "highline/import"
require "yaml"

class Person < Struct.new:)first_name, :last_name)
def self.parse( input )
if input =~ /^\s*(\w+),\s*(\w+)\s*$/
self.new($2, $1)
else
raise ArgumentError, "Invalid name format."
end
end
end

who = ask("Name? (last, first) ", Person)
y who

__END__

Hope that helps.

James Edward Gray II
 
D

Daniel Harple

Not to shanghai this convo (but its sort of on the same subject),
is there a
way to prompt for a response in-line with your question rather than
printing
the question and then having it go to the next line?

Yes, either set $stdout.sync = true, or call $stdout.flush.

-- Daniel
 
J

James Edward Gray II

Not to shanghai this convo (but its sort of on the same subject),
is there a
way to prompt for a response in-line with your question rather than
printing
the question and then having it go to the next line?

The HighLine example I posted does exactly that. The space at the
end of the question is a hint to HighLine that I'll take the answer
on the same line.

James Edward Gray II
 
J

James Edward Gray II

Yes, either set $stdout.sync = true, or call $stdout.flush.

Neither seem to be required:

$ ruby -e 'print "Name? "; gets; puts "Hello #$_"'
Name? James
Hello James

Ruby is a pretty clever girl. ;)

James Edward Gray II
 
D

David Ishmael

Perfect, thx!


-----Original Message-----
From: James Edward Gray II [mailto:[email protected]]
Sent: Friday, March 24, 2006 12:53 PM
To: ruby-talk ML
Subject: Re: Console IO

Yes, either set $stdout.sync = true, or call $stdout.flush.

Neither seem to be required:

$ ruby -e 'print "Name? "; gets; puts "Hello #$_"'
Name? James
Hello James

Ruby is a pretty clever girl. ;)

James Edward Gray II
 

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,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top