The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Ruby Programming
|
Newbie question
Discuss Newbie question in the Ruby Programming forum on Dev Shed. Newbie question Ruby and Ruby on Rails programming forum covering Ruby Tips and Tricks, Best Practices, and agile development with Ruby on Rails.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

March 4th, 2012, 06:00 AM
|
|
Registered User
|
|
Join Date: Mar 2012
Posts: 1
Time spent in forums: 29 m 45 sec
Reputation 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.
|

July 24th, 2012, 03:01 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Location: South Korea
Posts: 2
Time spent in forums: 1 h 3 m 14 sec
Reputation Power: 0
|
|
Quote: | 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
|

July 24th, 2012, 08:54 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
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).
|

July 26th, 2012, 03:51 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Location: South Korea
Posts: 2
Time spent in forums: 1 h 3 m 14 sec
Reputation Power: 0
|
|
Quote: | 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.
|

July 26th, 2012, 06:41 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
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.
|

July 30th, 2012, 05:23 AM
|
|
Registered User
|
|
Join Date: Jul 2012
Posts: 2
Time spent in forums: 28 m 58 sec
Reputation 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
|

July 30th, 2012, 06:27 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|