# Console IO

**URL:** <https://www.ruby-forum.com/t/console-io/86858>\
**Category:** Ruby\
**Created:** [March 24, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/console-io/86858 "2006-03-24T17:44:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 5:44pm UTC](https://www.ruby-forum.com/t/console-io/86858/1 "2006-03-24T17:44:10Z")

</div>

Is there a simpler way to do the following?

`
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
`

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.

`
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
`

Thanks,  
Shane

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 6:13pm UTC](https://www.ruby-forum.com/t/console-io/86858/2 "2006-03-24T18:13:10Z")

</div>

On Mar 24, 2006, at 10:43 AM, removed\_email\_address@domain.invalid wrote:

> 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 G. II

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 6:31pm UTC](https://www.ruby-forum.com/t/console-io/86858/3 "2006-03-24T18:31:41Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 6:36pm UTC](https://www.ruby-forum.com/t/console-io/86858/4 "2006-03-24T18:36:42Z")

</div>

On Mar 24, 2006, at 6:31 PM, David I. wrote:

> 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

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 6:53pm UTC](https://www.ruby-forum.com/t/console-io/86858/5 "2006-03-24T18:53:29Z")

</div>

On Mar 24, 2006, at 11:36 AM, Daniel H. wrote:

> On Mar 24, 2006, at 6:31 PM, David I. wrote:
> 
> > 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.

Neither seem to be required:

$ ruby -e ‘print "Name? "; gets; puts “Hello #$\_”’  
Name? James  
Hello James

Ruby is a pretty clever girl. 😉

James Edward G. II

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 6:51pm UTC](https://www.ruby-forum.com/t/console-io/86858/6 "2006-03-24T18:51:47Z")

</div>

On Mar 24, 2006, at 11:31 AM, David I. wrote:

> 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 G. II

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 7:14pm UTC](https://www.ruby-forum.com/t/console-io/86858/7 "2006-03-24T19:14:47Z")

</div>

Thanks James that’s a really nice solution.

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [March 24, 2006, 6:58pm UTC](https://www.ruby-forum.com/t/console-io/86858/8 "2006-03-24T18:58:47Z")

</div>

Perfect, thx!
