5 Replies - 3985 Views - Last Post: 07 December 2007 - 07:21 PM Rate Topic: -----

#1 TrueDego   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-November 07

Help with first program!

Posted 04 November 2007 - 05:28 PM

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.

=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

Is This A Good Question/Topic? 0
  • +

Replies To: Help with first program!

#2 TrueDego   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 03-November 07

Re: Help with first program!

Posted 04 November 2007 - 07:00 PM

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:

  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.
Was This Post Helpful? 0
  • +
  • -

#3 spullen   User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 356
  • Joined: 22-March 07

Re: Help with first program!

Posted 05 November 2007 - 02:59 PM

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:
puts (60 * 24).to_s + " Minutes in a Day"


Was This Post Helpful? 0
  • +
  • -

#4 brawnyman713   User is offline

  • D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 139
  • Joined: 21-October 07

Re: Help with first program!

Posted 06 November 2007 - 05:31 PM

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:
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):
puts "There are " + (60 * 24) + " minutes in a day"


and repeat for weeks, and years.
Was This Post Helpful? 0
  • +
  • -

#5 spullen   User is offline

  • D.I.C Regular
  • member icon

Reputation: 10
  • View blog
  • Posts: 356
  • Joined: 22-March 07

Re: Help with first program!

Posted 11 November 2007 - 04:55 PM

That won't work, it will throw an error, unless you convert it to a string using the to_s method.
Was This Post Helpful? 0
  • +
  • -

#6 nMiller92   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 07-December 07

Re: Help with first program!

Posted 07 December 2007 - 07:21 PM

This is how I did mine.

# 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."


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1