summerSkies

/r/40daysofruby ruby mini-assignment 1

Dec 30th, 2013
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 2.89 KB | None | 0 0
  1. # I'm not good at math so I'm pretty sure I did the calculations wrong
  2.  
  3. # hours in a year -----------------------------------------------------
  4. def hoursInYear
  5.     24*365
  6. end
  7.  
  8. # If I am 1031 million seconds old, how old am I?  ---------------------
  9. def secondsInYears(age_in_seconds)
  10.     age_in_seconds  / (60 * 60 *24 * 365 )
  11. end
  12.  
  13. # minutes in a decade
  14. def minsInDecade
  15.     60*24*7*4*12
  16. end
  17.  
  18. # how many seconds are you  --------------------------------------------
  19. def ageInSeconds(age)
  20.     60*60*24*7*4*12*age
  21.     # Not accounting for partial years
  22. end
  23.  
  24. # chocolates will you eat in your life ---------------------------------
  25. def chocolatesInLifetime(amount)
  26.     # assume they'll live to 90?
  27.     # there are 52 weeks in a year
  28.     # assume the estimate includes past eaten chocolates
  29.     52 * 90 * amount
  30.     # Doesn't account for 0 chocolates per week, since I don't know
  31.     # how many they've eaten in the past
  32. end
  33.  
  34. # ask first middle last names  ------------------------------------------
  35. def greeting(first, middle, last)
  36.     puts "Hello #{first} #{middle} #{last}."
  37. end
  38.  
  39.  
  40. # Table of contents  ---------------------------------------------------
  41. book = "Think Like a Programmer: An Introducion to Creative Problem Solving"
  42. author = "V. Anton Spraul"
  43. # The website doesn't list the pages so I just made them up.
  44. # toc = table of contents
  45. toc = {"Strategies for Problem Solving"=> 1, "Pure Puzzles"=> 2, "Problems with Arrays"=> 3, "Solving Problems with Pointers and Dynamic Memory"=> 4, "Solving Problems with Classes"=> 5, "Solving Problems with Recursion"=> 6, "Solving Problems with Code Reuse"=> 7, "Thinking Like a Programmer"=> 8}
  46.  
  47. # Now let's do some stuff!
  48.  
  49. puts    "There are  #{hoursInYear} hours in a year."
  50. puts    "And #{secondsInYears(1031000000)} years in 1031 million seconds!"
  51. puts    "There are #{minsInDecade} minutes in a decade."
  52. puts    "Hello, what's your name?"
  53. puts    "First name:"
  54.         first_name = gets.chomp
  55. puts    "Middle name:"
  56.         middle_name = gets.chomp
  57. puts    "Last name:"
  58.         last_name = gets.chomp
  59. puts    "Greetings, #{first_name} #{middle_name} #{last_name}!"
  60. puts    "How old are you?"
  61. age = gets.chomp
  62. puts    "You are #{ageInSeconds age.to_i} seconds old!"
  63. puts    "How many chocolates do you eat per week?"
  64. amount = gets.chomp
  65. puts    "You'll eat about #{chocolatesInLifetime amount.to_i} chocolates in your lifetime!"
  66. puts    "What's your favorite number, #{first_name}?"
  67. num = gets.chomp
  68. puts    "But #{num.to_i + 1} is so much better!"
  69. puts    "WHAT DO YOU WANT, #{first_name}?!"
  70. want = gets.chomp
  71. puts    "WHADDAYA MEAN YOU WANT #{want.upcase}?!?!?! GET OUTTA HERE!"
  72. puts    "Hey #{first_name}, here's a cool book I'm reading:"
  73. puts    "#{book} by #{author}"
  74. # None of the tutorials I read covered ljust/rjust usage
  75. # Compiler gives "undefined method `rjust'" error
  76. # So I'm just going to use tabs ( /t )
  77. puts    "Here's the chapters:"
  78. toc.each do | chapter, page|
  79.         puts "Page #{page} \t #{chapter}"
  80. end
Advertisement
Add Comment
Please, Sign In to add comment