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 May 26th, 2009, 05:01 PM
enzosf enzosf is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2009
Posts: 1 enzosf User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 16 m 8 sec
Reputation Power: 0
Adding Variables

Hello, I am just starting out with Ruby and I am reading a book and at the end of a chapter it gives me a little project but doesn't ask me what I did or tell me why it didn't work. So here it is...

I am trying to write a program that asks for a persons favorite car company and model and then at the end add up the letters and tell them how many there are. Not the characters but letters. Here is what I have got...

puts 'What\'s your favorite car company?'
name = gets.chomp
puts '' +name+ '\'s a great car company.'
puts 'What\'s your favorite model?'
name1 = gets.chomp
puts 'That\'s cool, ' +name1+ ' is a nice model.'
puts 'Did you know that ' +name+ ' ' +name1+ '
puts ' has ' +name.length.to_s + name1.length.to_s+ '
puts ' letters in it?'

So my issue is that it is adding the strings instead of the integers, I think. How do I make it add the integers to give me the full letter count of both names?

Thank you very much for any help. I am new at this.

Reply With Quote
  #2  
Old June 17th, 2009, 07:37 AM
SteffenL SteffenL is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 76 SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level)SteffenL User rank is First Lieutenant (10000 - 20000 Reputation Level) 
Time spent in forums: 12 h 35 m 35 sec
Reputation Power: 113
This is my first attempt to help someone with Ruby, and I barely know what Ruby is. Please excuse me if I am wrong, but I will take this opportunity to learn at least what Ruby code looks like.
If someone here feels like telling me "Don't answer if you don't know what you're doing", then please give the right answer instead.
However, it does look like you are converting name.length to a string with to_s(). You need to sum the integers instead. I don't know how (yet) but I would just guess something like this:
Code:
(name.length + name1.length).to_s()

I will have look around and let you know what I find. You can do that as well.

Edit:
Can you do it like this?
Code:
puts " has #{name.length + name1.length}"

Reply With Quote
  #3  
Old August 7th, 2009, 08:16 AM
theburger theburger is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2009
Posts: 16 theburger User rank is Sergeant Major (2000 - 5000 Reputation Level)theburger User rank is Sergeant Major (2000 - 5000 Reputation Level)theburger User rank is Sergeant Major (2000 - 5000 Reputation Level)theburger User rank is Sergeant Major (2000 - 5000 Reputation Level)theburger User rank is Sergeant Major (2000 - 5000 Reputation Level)theburger User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 15 h 21 m 37 sec
Reputation Power: 0
I would use
Code:
puts 'Did you know that '+name+' '+name1
puts 'has '+(name.length+name1.length).to_s+' letters in it'


The problem is that you have tried to add the string name.length to the string name1.length which is similar to trying to add 'fred' and 'bert'.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Adding Variables

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