Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
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:
Stop making mediocre tutorials.The best tutorials are video! Camtasia Studio makes it easy to create engaging, buzz-building screen videos at any size, in any popular format. Download the free trial!
  #1  
Old December 6th, 2006, 05:30 AM
Sean C. Sean C. is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2006
Posts: 1 Sean C. User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 m 4 sec
Reputation Power: 0
Hello, Newb Help please :)

basically i started programming yesterday..
and i have a quick question for anyone willing to help me out with what is probably a really newb poroblem.

okay, here it is, loops...

puts 'Hello Sonny Boy.'
greeting = gets.chomp

while greeting != greeting.upcase
puts 'HUH?! SPEAK UP, SONNY.'
greeting = gets.chomp
end

puts 'NO, NOT SINCE 1950!'
puts 'Anything else you would like to say to me?'
response = gets.chomp

while response != response.upcase
puts 'HUH?! SPEAK UP, SONNY!'
response = gets.chomp
end



So is there a way i can add those loops together? man this is confusing... another thing im stuck on is I want my grandma in the program to not let me stop talking to her until i say bye in upcase letters, as you can see in this line (while response != response.upcase) but i tried adding the string 'bye' to that after the (!=), but i couldnt figure out which way would let me run the program. is there a way to do that so i cant stop talking to her until i type bye in upcase letters? thanks for reading about my brain stumps.

Basically what im trying to do is:

write a deaf grandma program. Whatever i say to grandma (whatever i type in), she should respond with:

HUH?! SPEAK UP, SONNY!

unless i shout it, (type in all capitals). If i shout, she can hear me and yells back:

NO, NOT SINCE 1938.

(see i have this part done, but.

I want to not be able to stop talking to her until i shout bye (type bye in upcase letters).

Thanks for your help.

Reply With Quote
  #2  
Old December 6th, 2006, 11:39 PM
Joseph Taylor's Avatar
Joseph Taylor Joseph Taylor is offline
Text Ninja
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jun 2005
Location: Vancouver, British Columbia, Canada
Posts: 596 Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level)Joseph Taylor User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 1 Week 4 Days 1 h 12 m 43 sec
Reputation Power: 107
Send a message via Skype to Joseph Taylor Send a message via XFire to Joseph Taylor
I don't consider this any simpler, though it's most definitely shorter:

Code:
until (line = gets.chomp) == "BYE"
  puts (line == line.upcase ? "NO, NOT SINCE 1983!" : "HUH?! SPEAK UP, SONNY!")
end
puts "SO LONG CHESTER! DON'T FORGET YOUR LEMONADE!"


I'll do my best to explain what's going on in those four lines of ruby.
  • We enter an until loop. This executes the enclosed code until the condition (line = gets.chomp) == "BYE" is met
  • line = gets.chomp sets the variable line to input of the user's choosing (sans newline)
  • If the user typed "BYE" in all upper case letters the loop is complete, so Grandma says goodbye and the program exits
  • Otherwise the result of a [i]tertiary operator[i] ((line == line.upcase ? "NO, NOT SINCE 1983!" : "HUH?! SPEAK UP, SONNY!")) is printed to the screen and the test starts all over again. The syntax is probably a little scary looking to a novice, but it it's really just shorthand for a standard if/else construct:
    Code:
    if line == line.upcase
      puts "NO, NOT SINCE 1983!"
    else
      puts "HUH?! SPEAK UP, SONNY!"
    end

    The above code is trivial to break down (if the supplied line was a "SHOUT", say "NO, NOT SINCE 1983!", otherwise say "HUH?! SPEAK UP, SONNY") but I find it's a bit less elegant than the tertiary operator. That's just my subjective opinion though, take it for what it's worth.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Hello, Newb Help please :)


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

 Free IT White Papers!
 
Accelerating Trading Partner Performance
One in five. That's how many partner transactions have at least one error. That is an amazing statistic, particularly given the extraordinary leaps in innovation across the global supply chain during the past two decades. Download this white paper to learn more.

 
Competing on Analytics
This Tech Analysis is designed to help identify characteristics shared by analytics competitors, and includes information about 32 organizations that have made a commitment to quantitative, fact-based analysis.

 
Cost Effective Scaling with Virtualization and Coyote Point Systems
An overview of the industry trend toward virtualization, how server consolidation has increased the importance of application uptime and the steps being taken to integrate load balancing technology with virtualized servers.

 
Five Checkpoints to Implementing IP Telephony
Implementation planning for IP PBX software and IP telephony has become vital as businesses replace discontinued legacy PBX phone systems. This informative whitepaper outlines five "checkpoints" for any implementation plan that will help make IP communications a successful proposition.

 
Hosted Email Security: Staying Ahead of New Threats
In the last two years, email has become a fierce battleground between the nefarious forces of spam and malware, and the heroes of messaging protection. The spam volumes increased alarmingly every month, bringing clever new forms of phishing and virus propagation attacks.

 

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





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway