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 February 3rd, 2012, 11:27 PM
flebber flebber is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Location: Newcastle, Australia
Posts: 68 flebber User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 3 m 55 sec
Reputation Power: 8
Can't test empty string and exit

Hi

i was reading the chris Pine book and in attempting an exercise I cannot solve how to push a word into an array unless the input is an empty enter stroke in which case I should be printing the array sorted.

I have tried it 3 different ways clearly I am missing something, can you help put me in the right direction please.

Version 1 - Doesn't terminate on empty line
Code:
puts "enter a word one per line."
puts "enter a blank line if you want to end."
puts " >"  
word = gets.strip 
wordList = []  
wordList.push word until word.empty? == true 
if word.empty? == true   
     puts wordList.sort 
end


Version 2 - terminates on any input
Code:
puts "enter a word one per line."
puts "enter a blank line if you want to end."
puts " >"  
word = gets.strip 
wordList = []  
if word.empty? == true
 	wordList.push word 
else 	
        puts wordList.sort end


Version 3 - Also terminates on any input
Code:
puts "enter a word one per line."
puts "enter a blank line if you want to end." 
puts " >"  
word = gets.strip 
wordList = []  
if word != ''
 	wordList.push word 
else
 	puts wordList.sort end


I am little confused at where I am going wrong.

Reply With Quote
  #2  
Old February 5th, 2012, 04:12 AM
flebber flebber is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2006
Location: Newcastle, Australia
Posts: 68 flebber User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 13 h 3 m 55 sec
Reputation Power: 8
Here is a solution I have got working. if you know how it could work better let me know.

Code:
puts "Enter a word on a each new line."
puts "Press a blank new line to exit" 
puts "> "  
word = gets.strip 
wordList = [] 
while word.length > 0
     wordList.push(word)
     puts "> "
     word = gets.strip 
end  
puts wordList.sort

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Can't test empty string and exit

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