Another newbie question

I am trying to write a small little program that will currently run in
a command window (I will convert it later to GUI).

How do I request input from the user such as:

Please enter dollar amount:

I would like the prompt to display and the user to be able to enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.

Thanks
Len Sumnler

Citát len <lsumnler@gmail.com>:

I am trying to write a small little program that will currently run in
a command window (I will convert it later to GUI).

How do I request input from the user such as:

>>Please enter dollar amount:

I would like the prompt to display and the user to be able to enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.

Thanks
Len Sumnler

Well, this isn't BASIC, gets isn't supposed to display a prompt. Go like this:

  puts ">>>Please enter dollar amount:"
  amount = gets

David Vallner

len wrote:

How do I request input from the user such as:

>>Please enter dollar amount:

I would like the prompt to display and the user to be able to enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.

STDOUT.sync=true
print '>> Please enter amount: $'
resp = STDIN.gets.chomp
puts resp # echo input
puts "\nYour acount balance is below $" << resp << " :-?"

#-> Please enter amount: $12
#->
#-> Your acount balance is below $12 :-?

daz

len wrote:

I am trying to write a small little program that will currently run in
a command window (I will convert it later to GUI).

How do I request input from the user such as:

>>Please enter dollar amount:

I would like the prompt to display and the user to be able to enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.

Thanks
Len Sumnler

Thanks everyone for your responses. At this time, and with my level of
experience (none), I think the option I am looking for is the "puts"
command.

Again thanks everyone for your responses I have copied all of them for
future reference.

Len Sumnler

Hi --

···

On Fri, 12 Aug 2005 david@vallner.net wrote:

Citát len <lsumnler@gmail.com>:

I am trying to write a small little program that will currently run in
a command window (I will convert it later to GUI).

How do I request input from the user such as:

Please enter dollar amount:

I would like the prompt to display and the user to be able to enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.

Thanks
Len Sumnler

Well, this isn't BASIC, gets isn't supposed to display a prompt. Go like this:

puts ">>>Please enter dollar amount:"
amount = gets

Or use print instead of puts, if you want the input on the same line
as the prompt.

David

--
David A. Black
dblack@wobblini.net

Or use the readline library, there you can even supply the prompt and
you get a ton of extras.

regards,

Brian

···

On 12/08/05, daz <dooby@d10.karoo.co.uk> wrote:

len wrote:
>
> How do I request input from the user such as:
>
> >>Please enter dollar amount:
>
> I would like the prompt to display and the user to be able to enter the
> data after the prompt. I thought "gets" would do it but I don't
> understand how to get the prompt to display.
>

STDOUT.sync=true
print '>> Please enter amount: $'
resp = STDIN.gets.chomp
puts resp # echo input
puts "\nYour acount balance is below $" << resp << " :-?"

#-> Please enter amount: $12
#->
#-> Your acount balance is below $12 :-?

daz

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

STDOUT.sync = true isn't necessary in that snippet. Even though you
haven't output a newline, the outbuffer will get flushed as soon as
you call gets.

Bill

···

On 8/12/05, daz <dooby@d10.karoo.co.uk> wrote:

len wrote:
>
> How do I request input from the user such as:
>
> >>Please enter dollar amount:
>
> I would like the prompt to display and the user to be able to enter the
> data after the prompt. I thought "gets" would do it but I don't
> understand how to get the prompt to display.
>

STDOUT.sync=true
print '>> Please enter amount: $'
resp = STDIN.gets.chomp
puts resp # echo input
puts "\nYour acount balance is below $" << resp << " :-?"

#-> Please enter amount: $12
#->
#-> Your acount balance is below $12 :-?

daz

--
Bill Atkins

You'd probably want

puts "Please enter dollar amount":
gets line

Julian.

···

On 13/08/2005, at 12:41 AM, len wrote:

len wrote:

I am trying to write a small little program that will currently run in
a command window (I will convert it later to GUI).

How do I request input from the user such as:

Please enter dollar amount:

I would like the prompt to display and the user to be able to enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.

Thanks
Len Sumnler

Thanks everyone for your responses. At this time, and with my level of
experience (none), I think the option I am looking for is the "puts"
command.

Again thanks everyone for your responses I have copied all of them for
future reference.

Len Sumnler

How do I request input from the user such as:

Please enter dollar amount:

I would like the prompt to display and the user to be able to enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.

]

Do this first:
$ sudo gem install highline

Then you can get nice prompts like this:

#!/usr/local/bin/ruby

require "rubygems"
require "highline/import"

dollar_amt = ask("Please enter the dollar amount : ")

Then you can just use dollar_amt however you want and when you rub your program it will prompt for the input and once it gets it it will assign it to dollar_amt. Nice and simple. Thanks to James and Greg.

HTH-
-Ezra Zygmuntowicz
Yakima Herald-Republic
WebMaster
509-577-7732
ezra@yakima-herald.com

Bill Atkins wrote:

STDOUT.sync = true isn't necessary in that snippet. Even though you
haven't output a newline, the outbuffer will get flushed as soon as
you call gets.

That's not a feature of Ruby 1.8.2
You may be describing *nix behaviour ?

On Windows, it looks as if the program is hanging unless #sync of #flush
is used.

[What's worrying is that I can't manage to spell acccount]

daz

Hi --

You'd probably want

puts "Please enter dollar amount":
gets line

I doubt it, unless you've got some reason to expect the input to
terminate with whatever line contains :slight_smile:

David

···

On Fri, 12 Aug 2005, Julian Leviston wrote:

Julian.

On 13/08/2005, at 12:41 AM, len wrote:

len wrote:

I am trying to write a small little program that will currently run in
a command window (I will convert it later to GUI).

How do I request input from the user such as:

Please enter dollar amount:

I would like the prompt to display and the user to be able to enter the
data after the prompt. I thought "gets" would do it but I don't
understand how to get the prompt to display.

Thanks
Len Sumnler

Thanks everyone for your responses. At this time, and with my level of
experience (none), I think the option I am looking for is the "puts"
command.

Again thanks everyone for your responses I have copied all of them for
future reference.

Len Sumnler

--
David A. Black
dblack@wobblini.net

a minor correction:

···

On 12/08/05, Julian Leviston <julian@coretech.net.au> wrote:

You'd probably want

- puts "Please enter dollar amount":
+ puts "Please enter dollar amount:"
gets line

regards,

Brian

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

Or you could use Rio (http://rio.rubyforge.org)

amt = rio(?-).print("Please enter dollar amount: ").chomp.gets

That's strange - it doesn't hang on Windows for me.

···

On 8/12/05, daz <dooby@d10.karoo.co.uk> wrote:

Bill Atkins wrote:

> STDOUT.sync = true isn't necessary in that snippet. Even though you
> haven't output a newline, the outbuffer will get flushed as soon as
> you call gets.

That's not a feature of Ruby 1.8.2
You may be describing *nix behaviour ?

On Windows, it looks as if the program is hanging unless #sync of #flush
is used.

[What's worrying is that I can't manage to spell acccount]

daz

--
Bill Atkins

Hi --

···

On Sat, 13 Aug 2005, [ISO-8859-1] Brian Schröder wrote:

a minor correction:

On 12/08/05, Julian Leviston <julian@coretech.net.au> wrote:

You'd probably want

- puts "Please enter dollar amount":
+ puts "Please enter dollar amount:"
gets line

I'm almost certain he actually wants:

   line = gets

David

--
David A. Black
dblack@wobblini.net

Thank you all.

Right now I just a newbie trying to write some simple programs to get
the hang of the language. For now

puts 'Please enter the bill amount'
amount = gets

will do nicely. One must learn to walk before you can run and jumping
tall building in a single bound.................maybe 18 months away:)

Len Sumnler

Quite true, blush :wink:

brian

···

On 12/08/05, David A. Black <dblack@wobblini.net> wrote:

Hi --

On Sat, 13 Aug 2005, [ISO-8859-1] Brian Schröder wrote:

> a minor correction:
>
> On 12/08/05, Julian Leviston <julian@coretech.net.au> wrote:
>> You'd probably want
>
> - puts "Please enter dollar amount":
> + puts "Please enter dollar amount:"
> gets line

I'm almost certain he actually wants:

   line = gets

--
http://ruby.brian-schroeder.de/

Stringed instrument chords: http://chordlist.brian-schroeder.de/

David A. Black wrote:

···

On Sat, 13 Aug 2005, [ISO-8859-1] Brian Schröder wrote:

> a minor correction:
>
> On 12/08/05, Julian Leviston wrote:
>> You'd probably want
>
> - puts "Please enter dollar amount":
> + puts "Please enter dollar amount:"
> gets line

I'm almost certain he actually wants:

   line = gets

raise 'almost certain' # :slight_smile:

------------------------------------------------------------ Kernel#gets
     gets(separator=$/) => string or nil
------------------------------------------------------------------------
     Returns (and assigns to $_) the next line from the list of files
     in ARGV (or $*), or from standard input if no files are present
     on the command line.
     [...]

STDIN.gets is safer for this case, I think.

daz

Hi --

David A. Black wrote:

a minor correction:

You'd probably want

- puts "Please enter dollar amount":
+ puts "Please enter dollar amount:"
gets line

I'm almost certain he actually wants:

   line = gets

raise 'almost certain' # :slight_smile:

------------------------------------------------------------ Kernel#gets
    gets(separator=$/) => string or nil
------------------------------------------------------------------------
    Returns (and assigns to $_) the next line from the list of files
    in ARGV (or $*), or from standard input if no files are present
    on the command line.
    [...]

Yes -- hence my original reply:

   I doubt it, unless you've got some reason to expect the input to
   terminate with whatever line contains :slight_smile:

STDIN.gets is safer for this case, I think.

What would be the non-safeness of gets?

David

···

On Sat, 13 Aug 2005, daz wrote:

On Sat, 13 Aug 2005, [ISO-8859-1] Brian Schröder wrote:

On 12/08/05, Julian Leviston wrote:

--
David A. Black
dblack@wobblini.net

David A. Black wrote

> David A. Black wrote:
>
>>
>>> a minor correction:
>>>
>>>> You'd probably want
>>>
>>> - puts "Please enter dollar amount":
>>> + puts "Please enter dollar amount:"
>>> gets line
>>
>> I'm almost certain he actually wants:
>>
>> line = gets
>
>
> raise 'almost certain' # :slight_smile:
>
> ------------------------------------------------------------ Kernel#gets
> gets(separator=$/) => string or nil
> ------------------------------------------------------------------------
> Returns (and assigns to $_) the next line from the list of files
> in ARGV (or $*), or from standard input if no files are present
> on the command line.
> [...]

Yes -- hence my original reply:

   I doubt it, unless you've got some reason to expect the input to
   terminate with whatever line contains :slight_smile:

I wasn't referring to your 'separator' note.

> STDIN.gets is safer for this case, I think.

What would be the non-safeness of gets?

David

Kernel#gets has a higher action when ARGV is non-empty.

···

On Sat, 13 Aug 2005, daz wrote:
>> On Sat, 13 Aug 2005, [ISO-8859-1] Brian Schröder wrote:
>>> On 12/08/05, Julian Leviston wrote:

#----------------------
ARGV.replace ["anyarg"]
line = gets # <--- STDIN.gets ---*
#----------------------

#-> C:/TEMP/rb80B3.TMP:2:in `gets': No such file or directory - anyarg (Errno::ENOENT)

Quoting the OP:
  "I would like the prompt to display and the user to be able to enter the
   data after the prompt."

No mention of: "... unless he supplies a C/L argument, in which case
treat it as a file and read the input from there instead."

Therefore:
  > > STDIN.gets is safer for this case, I think.

daz