Forum: Ruby Console IO

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
12a71a456ac3d464914a8267f11d43b3?d=identicon&s=25 semmons99@gmail.com (Guest)
on 2006-03-24 17:44
(Received via mailing list)
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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-24 18:13
(Received via mailing list)
On Mar 24, 2006, at 10:43 AM, semmons99@gmail.com 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 Gray II
2007c0c37cb176dc3d601cf5724fea54?d=identicon&s=25 David Ishmael (Guest)
on 2006-03-24 18:31
(Received via mailing list)
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
9358cc96c46055cd68d4a76a9aefe026?d=identicon&s=25 Daniel Harple (Guest)
on 2006-03-24 18:36
(Received via mailing list)
On Mar 24, 2006, at 6:31 PM, David Ishmael 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
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-24 18:51
(Received via mailing list)
On Mar 24, 2006, at 11:31 AM, David Ishmael 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 Gray II
4299e35bacef054df40583da2d51edea?d=identicon&s=25 James Gray (bbazzarrakk)
on 2006-03-24 18:53
(Received via mailing list)
On Mar 24, 2006, at 11:36 AM, Daniel Harple wrote:

> On Mar 24, 2006, at 6:31 PM, David Ishmael 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 Gray II
2007c0c37cb176dc3d601cf5724fea54?d=identicon&s=25 David Ishmael (Guest)
on 2006-03-24 18:58
(Received via mailing list)
Perfect, thx!
12a71a456ac3d464914a8267f11d43b3?d=identicon&s=25 semmons99@gmail.com (Guest)
on 2006-03-24 19:14
(Received via mailing list)
Thanks James that's a really nice solution.
This topic is locked and can not be replied to.