Advertisement
Guest User

Untitled

a guest
Jan 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. # suplement exercises
  2.  
  3. # return "HELLO <your name>!" in this method
  4. # return "HELLO WORLD!" if name is empty
  5. # this method is shouting so you just need
  6. # need to upcase the result
  7. def shout(name)
  8. name = 'WORLD' if name.empty?
  9. "HELLO #{name.upcase}!"
  10. end
  11.  
  12. # return "Drink served!" if the birth date was more
  13. # than 18 years ago.
  14. # return "Go Home!" if is below 18 years or "Drink Served"
  15. # otherwise
  16. def can_drink(birth_date)
  17. age = (Date.today - birth_date).to_i / 365
  18. if age >= 18
  19. 'Drink Served!'
  20. else
  21. 'Go Home!'
  22. end
  23. end
  24.  
  25. # return the average number between min and max in the Float format
  26. # # be carefull! sometimes they will be a string that needs
  27. # to be converted
  28. def average(min, max)
  29. sum = 0
  30. (min..max).each do |i|
  31. sum += i
  32. end
  33. sum.to_f / (max - min + 1)
  34. end
  35.  
  36. # return the multiplication table of a number given
  37. # in the parameters up until he reaches max_multiplier
  38. # # it should return a string with the following format
  39. # (example for number 3 and max_multiplier 10)
  40. # 1x3=3
  41. # 2x3=6
  42. # 3x3=9
  43. # 3x4=12
  44. # ...
  45. # 3x10=30
  46. # this method should return a string with each
  47. # computation in each line. you can use \n to do a new line
  48. def multiplication_table(number, max_multiplier)
  49. table = []
  50. (1..max_multiplier).each do |i|
  51. table << "#{number}x#{i}=#{number * i}"
  52. end
  53. table.join("\n")
  54. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement