Advertisement
Guest User

Untitled

a guest
Apr 25th, 2019
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.63 KB | None | 0 0
  1. # 1
  2.  
  3. # inputs [1,2,3,4,5], 2
  4.  
  5. # outputs [2,4,6,8,10]
  6.  
  7. def do_something(arr_of_numbers, multiplier)
  8. result = arr_of_numbers.map do |num|
  9. num * multiplier
  10. end
  11. return result
  12. # result = []
  13. # arr_of_numbers.each do |num|
  14. # result << (num * multiplier)
  15. # end
  16. end
  17.  
  18. # 2
  19.  
  20. # input1 [2,3,4,5,6], 3
  21.  
  22. do_something([2,3,4,5,6], 3)
  23.  
  24. # 3
  25.  
  26. class Student
  27. attr_accessor :name
  28. # attr_reader :name
  29. # attr_writer :name
  30. def initialize(name, age, gender)
  31. @name = name
  32. @age = age
  33. @gender = gender
  34. end
  35. end
  36.  
  37. # 4
  38.  
  39. harrison = Student.new('harrison', 24, 'male')
  40.  
  41. # 5
  42.  
  43. tess = Student.new('tess', 25, 'female')
  44. tess.name = 'Tess'
  45.  
  46. # 6
  47.  
  48. def first_i_was_afraid(arr_of_strings, word_to_check)
  49. # arr_of_strings.each do |word|
  50. # if word == word_to_check
  51. # return true
  52. # end
  53. # end
  54. # return false
  55. return arr_of_strings.include?(word_to_check)
  56. end
  57.  
  58. first_i_was_afraid(['i', 'will', 'survive'], 'survive')
  59. # false
  60.  
  61. # 7
  62.  
  63. def div_by_7?(num)
  64. if num % 7 == 0
  65. return true
  66. else
  67. return false
  68. end
  69. end
  70.  
  71. div_by_7?(14)
  72.  
  73. # 8
  74.  
  75. def array_splitter(array_of_nums)
  76. div_by_7 = []
  77. not_div_by_7 = []
  78. array_of_nums.each do |num|
  79. if div_by_7?(num)
  80. div_by_7 << num
  81. else
  82. not_div_by_7 << num
  83. end
  84. end
  85. return [div_by_7, not_div_by_7]
  86. end
  87.  
  88. array_splitter([1,2,3,4,5,6,7])
  89.  
  90. # 9
  91.  
  92. def closing_time(name)
  93. return "Thanks #{name} for everything"
  94. end
  95.  
  96. closing_time('harrison')
  97.  
  98. # 10
  99.  
  100. def pizza_time(string)
  101. if string.include?('pizza')
  102. return 'Pizza time'
  103. else
  104. return 'Meatloaf'
  105. end
  106. end
  107.  
  108. pizza_time('I got 99 problems but a pizza aint one')
  109. # Pizza time
  110. pizza_time('Home now')
  111. # Mealoaf
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement