Another newbie question

Discussion in 'Ruby' started by len, Aug 12, 2005.

  1. len

    len Guest

    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
     
    len, Aug 12, 2005
    #1
    1. Advertising

  2. len

    Guest

    Cit=E1t len <>:

    > I am trying to write a small little program that will currently run in
    > a command window (I will convert it later to GUI).
    >=20
    > How do I request input from the user such as:
    >=20
    > >>Please enter dollar amount:

    >=20
    > 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.
    >=20
    > Thanks
    > Len Sumnler
    >=20
    >=20
    >=20



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

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

    David Vallner
     
    , Aug 12, 2005
    #2
    1. Advertising

  3. --8323328-1636550047-1123853742=:12576
    Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1636550047-1123853742=:12576"

    This message is in MIME format. The first part should be readable text,
    while the remaining parts are likely unreadable without MIME-aware tools.

    --8323328-1636550047-1123853742=:12576
    Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
    Content-Transfer-Encoding: QUOTED-PRINTABLE

    Hi --

    On Fri, 12 Aug 2005 wrote:

    > Cit=E1t len <>:
    >
    >> 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 =3D gets


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


    David

    --=20
    David A. Black

    --8323328-1636550047-1123853742=:12576--
    --8323328-1636550047-1123853742=:12576--
     
    David A. Black, Aug 12, 2005
    #3
  4. len

    daz Guest

    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
     
    daz, Aug 12, 2005
    #4
  5. On 12/08/05, daz <> wrote:
    >=20
    > 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.
    > >

    >=20
    >=20
    > STDOUT.sync=3Dtrue
    > print '>> Please enter amount: $'
    > resp =3D STDIN.gets.chomp
    > puts resp # echo input
    > puts "\nYour acount balance is below $" << resp << " :-?"
    >=20
    > #-> Please enter amount: $12
    > #->
    > #-> Your acount balance is below $12 :-?
    >=20
    >=20
    > daz
    >=20
    >=20
    >=20
    >=20
    >=20


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

    regards,

    Brian

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

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
     
    Brian Schröder, Aug 12, 2005
    #5
  6. len

    Bill Atkins Guest

    STDOUT.sync =3D 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 <> wrote:
    >=20
    > 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.
    > >

    >=20
    >=20
    > STDOUT.sync=3Dtrue
    > print '>> Please enter amount: $'
    > resp =3D STDIN.gets.chomp
    > puts resp # echo input
    > puts "\nYour acount balance is below $" << resp << " :-?"
    >=20
    > #-> Please enter amount: $12
    > #->
    > #-> Your acount balance is below $12 :-?
    >=20
    >=20
    > daz
    >=20
    >=20
    >=20
    >=20
    >=20



    --=20
    Bill Atkins
     
    Bill Atkins, Aug 12, 2005
    #6
  7. len

    len Guest

    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
     
    len, Aug 12, 2005
    #7
  8. 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
    >
    >
    >
     
    Julian Leviston, Aug 12, 2005
    #8
  9. len

    daz Guest

    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
     
    daz, Aug 12, 2005
    #9
  10. Hi --

    On Fri, 12 Aug 2005, Julian Leviston wrote:

    > 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 :)


    David

    > 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
     
    David A. Black, Aug 12, 2005
    #10
  11. Brian Schröder, Aug 12, 2005
    #11
  12. len

    Bill Atkins Guest

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

    On 8/12/05, daz <> wrote:
    >=20
    > Bill Atkins wrote:
    >=20
    > > STDOUT.sync =3D true isn't necessary in that snippet. Even though yo=

    u
    > > haven't output a newline, the outbuffer will get flushed as soon as
    > > you call gets.

    >=20
    >=20
    > That's not a feature of Ruby 1.8.2
    > You may be describing *nix behaviour ?
    >=20
    > On Windows, it looks as if the program is hanging unless #sync of #flush
    > is used.
    >=20
    > [What's worrying is that I can't manage to spell acccount]
    >=20
    >=20
    > daz
    >=20
    >=20
    >=20
    >=20
    >=20



    --=20
    Bill Atkins
     
    Bill Atkins, Aug 12, 2005
    #12
  13. --8323328-1305152715-1123865835=:16311
    Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-1305152715-1123865835=:16311"

    This message is in MIME format. The first part should be readable text,
    while the remaining parts are likely unreadable without MIME-aware tools.

    --8323328-1305152715-1123865835=:16311
    Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
    Content-Transfer-Encoding: QUOTED-PRINTABLE

    Hi --

    On Sat, 13 Aug 2005, [ISO-8859-1] Brian Schr=F6der 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 =3D gets


    David

    --=20
    David A. Black

    --8323328-1305152715-1123865835=:16311--
    --8323328-1305152715-1123865835=:16311--
     
    David A. Black, Aug 12, 2005
    #13
  14. >
    >>
    >> 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 Zygmuntowicz, Aug 12, 2005
    #14
  15. On 12/08/05, David A. Black <> wrote:
    > Hi --
    >=20
    > On Sat, 13 Aug 2005, [ISO-8859-1] Brian Schr=F6der wrote:
    >=20
    > > 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

    >=20
    > I'm almost certain he actually wants:
    >=20
    > line =3D gets
    >=20


    Quite true, blush ;)

    brian

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

    Stringed instrument chords: http://chordlist.brian-schroeder.de/
     
    Brian Schröder, Aug 12, 2005
    #15
  16. len

    daz Guest

    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' # :)


    ------------------------------------------------------------ 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
     
    daz, Aug 12, 2005
    #16
  17. --8323328-764081161-1123876667=:21614
    Content-Type: MULTIPART/MIXED; BOUNDARY="8323328-764081161-1123876667=:21614"

    This message is in MIME format. The first part should be readable text,
    while the remaining parts are likely unreadable without MIME-aware tools.

    --8323328-764081161-1123876667=:21614
    Content-Type: TEXT/PLAIN; charset=X-UNKNOWN; format=flowed
    Content-Transfer-Encoding: QUOTED-PRINTABLE

    Hi --

    On Sat, 13 Aug 2005, daz wrote:

    >
    > David A. Black wrote:
    >
    >> On Sat, 13 Aug 2005, [ISO-8859-1] Brian Schr=F6der 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 =3D gets

    >
    >
    > raise 'almost certain' # :)
    >
    > ------------------------------------------------------------ Kernel#gets
    > gets(separator=3D$/) =3D> 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 :)

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


    What would be the non-safeness of gets?


    David

    --=20
    David A. Black

    --8323328-764081161-1123876667=:21614--
    --8323328-764081161-1123876667=:21614--
     
    David A. Black, Aug 12, 2005
    #17
  18. len

    rio4ruby Guest

    rio4ruby, Aug 13, 2005
    #18
  19. len

    daz Guest

    David A. Black wrote

    > On Sat, 13 Aug 2005, daz wrote:
    >
    > > 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' # :)
    > >
    > > ------------------------------------------------------------ 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 :)
    >



    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.

    #----------------------
    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
     
    daz, Aug 13, 2005
    #19
  20. len

    len Guest

    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
     
    len, Aug 14, 2005
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Max
    Replies:
    2
    Views:
    464
    Ralf Hildebrandt
    Sep 1, 2003
  2. Dave Newberry
    Replies:
    0
    Views:
    668
    Dave Newberry
    Oct 17, 2004
  3. SStory
    Replies:
    1
    Views:
    295
    Kevin Spencer
    Aug 4, 2003
  4. Rob R. Ainscough
    Replies:
    3
    Views:
    355
    Rob R. Ainscough
    Jun 14, 2005
  5. Jerry C.
    Replies:
    8
    Views:
    264
    Uri Guttman
    Nov 23, 2003
Loading...

Share This Page