Thread: Newbie question

    #1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Newbie question


    hello everyone,
    here i have this little script:
    Code:
    def compare
    puts "Enter a word or a phrase: "
    val = gets.chomp!
    val1=val.downcase.reverse.gsub(/\W/,"")
    if val1 == val1.reverse
    print "Its a palindrome :)"
    else print "Its not a palindrome "
    end
    compare
    end
    i wondering how i can make it run only once ? i want it to work like i give a word, script checks it, gives me an answer and stops. because now after all this it asks me to give another word again and again.
  2. #2
  3. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Location
    South Korea
    Posts
    2
    Rep Power
    0
    Originally Posted by ArXi
    hello everyone,
    here i have this little script:
    Code:
    def compare
    puts "Enter a word or a phrase: "
    val = gets.chomp!
    val1=val.downcase.reverse.gsub(/\W/,"")
    if val1 == val1.reverse
    print "Its a palindrome :)"
    else print "Its not a palindrome "
    end
    compare
    end
    i wondering how i can make it run only once ? i want it to work like i give a word, script checks it, gives me an answer and stops. because now after all this it asks me to give another word again and again.
    Your code is recursive. so you should remove compare from the def. the code will be
    Code:
    def compare
    puts "Enter a word or a phrase: "
    val = gets.chomp!
    val1=val.downcase.reverse.gsub(/\W/,"")
    if val1 == val1.reverse
    print "Its a palindrome :)"
    else print "Its not a palindrome "
    end
    end
    
    compare
  4. #3
  5. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,910
    Rep Power
    1045
    Hi,

    This recursion is wrong, anyway. Since the methods never return, the call stack just keeps growing until it's eventually full (raising a SystemStackError).

    You'd have to use an endless loop for that (or advanced features like Fibers).
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Location
    South Korea
    Posts
    2
    Rep Power
    0
    Originally Posted by Jacques1
    Hi,

    This recursion is wrong, anyway. Since the methods never return, the call stack just keeps growing until it's eventually full (raising a SystemStackError).

    You'd have to use an endless loop for that (or advanced features like Fibers).
    ArXi doesn't want repetition of input requests from the program.
    He doesn't want recursion.
  8. #5
  9. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,910
    Rep Power
    1045
    Yes, I did read that, and you already answered his question, didn't you?

    My point is that he mustn't ever use this looping approach. So even when one day he actually wants to do an endless loop (not this time), he mustn't use recursion.

    I hope that clears it up.
  10. #6
  11. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0
    def compare
    puts "Enter a word or a phrase: "
    val = gets.chomp!
    val.length
    if(val.length == 0)
    puts "-----------------------"
    puts "Your input is not Valid"
    puts "-----------------------"
    return compare()
    end
    val1=val.downcase.reverse.gsub(/\W/,"")
    if val1 == val1.reverse
    print "Its a palindrome "
    else
    print "Its not a palindrome "
    end
    end


    compare()

    By
    bdeveloper01
  12. #7
  13. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,910
    Rep Power
    1045
    Well, that's the exact same error we were talking about.

    Do not use recursion for potentially endless loops (unless the language supports it and you know what you're doing). Sure, a user probably won't hit Enter 1000 times. But it's still very bad style, because if he actually does, the call stack will blow up.

    Use a while loop or exception handling:
    Code:
    puts 'Enter a word or a phrase:'
    input = gets.chomp
    while input.empty?
      puts 'Your input is not valid'
      input = gets.chomp
    end
    Code:
    InputError = Class.new StandardError
    
    puts 'Enter a word or a phrase:'
    begin
      input = gets.chomp
      raise InputError, 'Your input is not valid' if input.empty?
    rescue InputError => error
      puts error.message
      retry
    end

IMN logo majestic logo threadwatch logo seochat tools logo