Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 4th, 2012, 06:00 AM
ArXi ArXi is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2012
Posts: 1 ArXi User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #2  
Old July 24th, 2012, 03:01 AM
leo.jnag leo.jnag is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Location: South Korea
Posts: 2 leo.jnag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #3  
Old July 24th, 2012, 08:54 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2012
Location: Germany
Posts: 2,014 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 6 Days 11 h 26 m 31 sec
Reputation Power: 812
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).

Reply With Quote
  #4  
Old July 26th, 2012, 03:51 AM
leo.jnag leo.jnag is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Location: South Korea
Posts: 2 leo.jnag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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.

Reply With Quote
  #5  
Old July 26th, 2012, 06:41 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2012
Location: Germany
Posts: 2,014 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 6 Days 11 h 26 m 31 sec
Reputation Power: 812
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.

Reply With Quote
  #6  
Old July 30th, 2012, 05:23 AM
bdeveloper01 bdeveloper01 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2012
Posts: 2 bdeveloper01 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 28 m 58 sec
Reputation Power: 0
Send a message via Skype to bdeveloper01
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

Reply With Quote
  #7  
Old July 30th, 2012, 06:27 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Dev Shed Regular (2000 - 2499 posts)
 
Join Date: Jul 2012
Location: Germany
Posts: 2,014 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 6 Days 11 h 26 m 31 sec
Reputation Power: 812
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Newbie question

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap