Welcome to Dream.In.Code
Getting Help is Easy!

Join 109,157 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,026 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Help with first program!

 
Reply to this topicStart new topic

Help with first program!

TrueDego
post 4 Nov, 2007 - 05:28 PM
Post #1


New D.I.C Head

*
Joined: 3 Nov, 2007
Posts: 9


My Contributions


Ok, before I go any further, this is my own program. Im doing the learning Tutorial from LearningRuby.com and not from a text book or anything for a class, this is mine and only mine. With that said, I would like to know if anyone could help me figure out what Ive done wrong?

Before I reveal this life changing code, I used to be a BASIC (not VB) programmer so if you reconize old basic, this will look familiar. Now with ruby, can i set a variable for a number like in basic? Like 2 + 3 = A; A - 4 =B Etc..? Well, if anyone can help let me know, Ive been dying to learn this for a while now.

This program is supposed to tell you the amount of minutes in a year, but Ive taken it a bit further. Im having it tell me the amount of minutes in an Hour, the amount of minutes in a Day, In a week, and In a year. Thank you for your time.

CODE
=begin  
Ruby Numbers  
Usual operators:  
+ addition  
- subtraction  
* multiplication  
/ division  
=end  
  
puts "60 Minutes In an Hour"
puts 60 * 24 "Minutes in a Day"
puts ( 60 * 24 ) * 7 "Minutes in a Week"
puts ( 60 * 24  * 7) * 52 "Minutes in a Year"



Help if Possible!
-TD
User is offlineProfile CardPM

Go to the top of the page


TrueDego
post 4 Nov, 2007 - 07:00 PM
Post #2


New D.I.C Head

*
Joined: 3 Nov, 2007
Posts: 9


My Contributions



CODE
minutes_a_day = ( 60 * 24 )  
  365.times do |days|  
    puts "In #{days + 1} days there are #{minutes_a_day * (days + 1)} minutes"  
    end


I just wrote this with the help of my friend and I would like to know what you guys think. How else could I do it? As I said in the last post, I am using the tutorials from rubylearning and there was a code snipit from Peter Cooper's example in his Beginning Ruby book:

CODE
  rice_on_square = 1  
   64.times do |square|  
   puts "On square #{square + 1} are #{rice_on_square} grain(s)"  
   rice_on_square *= 2  
   end  


I modified it for the minutes in a year for each day * 365. I know I could have done a simple routine that just did the math to multiply the minutes* hours * day * week but that just didn't interest me. If anyone has any ideas on how I can make it more efficient let me know.
User is offlineProfile CardPM

Go to the top of the page

spullen
post 5 Nov, 2007 - 02:59 PM
Post #3


D.I.C Regular

Group Icon
Joined: 22 Mar, 2007
Posts: 330



Dream Kudos: 50
My Contributions


In your first part you need to concatenate the product with the strings or else it will throw errors.
To do that you need to do the math and put it all in parenthesis then call the function to_s then to concatenate it to the rest of the string you simply have to put a +.
Here is an example:
CODE

puts (60 * 24).to_s + " Minutes in a Day"
User is offlineProfile CardPM

Go to the top of the page

brawnyman713
post 6 Nov, 2007 - 05:31 PM
Post #4


D.I.C Head

Group Icon
Joined: 21 Oct, 2007
Posts: 92



Dream Kudos: 125
My Contributions


Actually, you could try do it this way. Say that you created a variable called a_number, and you set its value to 3. If you wanted to print "a_number has a value of: 3", you could say:
CODE

puts "A number has a value of: " + a_number

You could use this method to create a variable called days, weeks, and years, and print them that way. If you dont want to do the variables, you could do this(this is an example for days):
CODE

puts "There are " + (60 * 24) + " minutes in a day"

and repeat for weeks, and years.
User is offlineProfile CardPM

Go to the top of the page

spullen
post 11 Nov, 2007 - 04:55 PM
Post #5


D.I.C Regular

Group Icon
Joined: 22 Mar, 2007
Posts: 330



Dream Kudos: 50
My Contributions


That won't work, it will throw an error, unless you convert it to a string using the to_s method.
User is offlineProfile CardPM

Go to the top of the page

nMiller92
post 7 Dec, 2007 - 07:21 PM
Post #6


New D.I.C Head

*
Joined: 7 Dec, 2007
Posts: 3


My Contributions


This is how I did mine.

CODE

# minutes.rb
# Calculates the numbers of minutes in a year.

puts "There are 60 minutes in an hour..."
puts "24 hours in a day..."

min_day = 60 * 24
puts "#{min_day} minutes in one day..."

min_week = min_day * 7
puts "#{min_week} minutes in one week..."

min_month = min_week * 4
puts "#{min_month} minutes in one month..."

min_year = min_month * 12
puts "Therefore, #{min_year} minutes in one year."
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 9/5/08 05:32PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month