New user - very basic question

Discussion in 'Ruby' started by Ashley Wharton, Feb 7, 2008.

  1. [Note: parts of this message were removed to make it a legal post.]

    Hi - sorry to bother with such a basic question, but is there a better way
    to write the following: (before I move on I want to make sure I am following
    best practice)

    def require_number number_cur
    puts number_cur
    reply = gets.to_i

    while reply < 100
    reply = reply + 1
    puts reply
    end

    if reply > 100
    true
    require_number 'Please enter a number less than 100: '
    end
    end
    require_number 'Enter a number: '

    Thanks so much!
    Ashley Wharton, Feb 7, 2008
    #1
    1. Advertising

  2. Not elegant, but better:

    def require_number(prompt, max)
    print prompt
    reply = gets.to_i
    if reply >= max then reply = require_number "Please enter a number
    less than #{max}: ", max end
    reply
    end

    require_number 'Please enter a number: ', 100
    --
    Posted via http://www.ruby-forum.com/.
    Benjamin Stiglitz, Feb 7, 2008
    #2
    1. Advertising

  3. [Note: parts of this message were removed to make it a legal post.]

    On Feb 7, 2008 5:10 PM, Benjamin Stiglitz <> wrote:

    > Not elegant, but better:
    >
    > def require_number(prompt, max)
    > print prompt
    > reply = gets.to_i
    > if reply >= max then reply = require_number "Please enter a number
    > less than #{max}: ", max end
    > reply
    > end
    >
    > require_number 'Please enter a number: ', 100



    Thank you for taking the time - could I trouble you further? If I want
    the "reply" to iterate through until it reaches 100 could just add the
    "while" to your code as below?

    def require_number(prompt, max)
    print prompt
    reply = gets.to_i
    if reply >= max then reply = require_number "Please enter a number
    less than #{max}: ", max end
    while reply < max
    reply = reply + 1
    puts reply
    end
    end
    require_number 'Please enter a number: ', 100

    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Ashley Wharton, Feb 7, 2008
    #3
  4. > Thank you for taking the time - could I trouble you further? If I want
    > the "reply" to iterate through until it reaches 100 could just add the
    > "while" to your code as below?


    > while reply < max
    > reply = reply + 1
    > puts reply
    > end


    Even better is
    reply.upto(max - 1) { |x| puts x }

    -Ben

    --
    Posted via http://www.ruby-forum.com/.
    Benjamin Stiglitz, Feb 7, 2008
    #4
  5. [Note: parts of this message were removed to make it a legal post.]

    Thank you very much! Take care,

    ashley

    On Feb 7, 2008 6:13 PM, Benjamin Stiglitz <> wrote:

    > > Thank you for taking the time - could I trouble you further? If I want
    > > the "reply" to iterate through until it reaches 100 could just add the
    > > "while" to your code as below?

    >
    > > while reply < max
    > > reply = reply + 1
    > > puts reply
    > > end

    >
    > Even better is
    > reply.upto(max - 1) { |x| puts x }
    >
    > -Ben
    >
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >
    Ashley Wharton, Feb 8, 2008
    #5
    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. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    451
    Raymond Hettinger
    Jul 27, 2003
  2. aghazalp

    very very basic question

    aghazalp, Apr 2, 2006, in forum: Python
    Replies:
    6
    Views:
    334
    aghazalp
    Apr 2, 2006
  3. shanx__=|;-

    very very very long integer

    shanx__=|;-, Oct 16, 2004, in forum: C Programming
    Replies:
    19
    Views:
    1,592
    Merrill & Michele
    Oct 19, 2004
  4. Peter

    Very very very basic question

    Peter, Feb 8, 2005, in forum: C Programming
    Replies:
    14
    Views:
    498
    Dave Thompson
    Feb 14, 2005
  5. mdh

    A very **very** basic question

    mdh, Sep 25, 2008, in forum: C Programming
    Replies:
    57
    Views:
    1,191
    August Karlstrom
    Sep 26, 2008
Loading...

Share This Page