Guest User

Untitled

a guest
Nov 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.76 KB | None | 0 0
  1. # 1
  2. def new_cat(str1, str2)
  3. str1 + str2
  4. end
  5.  
  6. new_cat("Ken ", "Shimizu")
  7.  
  8. # 2
  9. def sum(nums_to_add)
  10. nums_to_add.inject(0, :+)
  11. end
  12.  
  13. # 3
  14. # "best" is at index 3.
  15. # "ruby" is at index 0.
  16. # The length is 7.
  17.  
  18. # 4
  19. class String
  20. def fun_string!
  21. 0.upto(self.length-1) { |index| self[index] = self[index].capitalize! if index.odd? }
  22. self.reverse!
  23. end
  24. end
  25.  
  26. # 5
  27. def fizzblam
  28. (1..100).each do |num|
  29. if num % 35 == 0 then puts "FizzBlam"
  30. elsif num % 5 == 0 then puts "Fizz"
  31. elsif num % 7 == 0 then puts "Blam"
  32. else puts num
  33. end
  34. end
  35. end
  36.  
  37. # 6
  38. class GuessingGame
  39. def initialize(answer)
  40. @answer = answer
  41. @guess = nil
  42. end
  43.  
  44. def guess(guess)
  45. @guess = guess
  46. if @guess > @answer then :high
  47. elsif @guess < @answer then :low
  48. else
  49. :correct
  50. end
  51. end
  52.  
  53. def solved?
  54. @answer == @guess
  55. end
  56. end
  57.  
  58. # 7
  59. # Global - Accessible by any class, instance, or method. Dangerous to use as any method or variable defined
  60. # in global scope can be easily overwritten
  61. # Class - Exists in the class itself. Instances generally have access to class scope, though some methods
  62. # defined in class scope are designated for only class access. E.g. Class methods (Array.new)
  63. # class variables
  64. # Instance - Exists within an instance of a class. E.g. Initializiation vars of an object used to keep
  65. # track of an instances traits. Can be accessed by methods within an instance but
  66. # not available outside the class without getters and setters.
  67. # Local - Exists only within the method or block that it is defined in. E.g. Create an array
  68. # within a method to shovel values into and then return at the end of the method.
  69.  
  70. # 8
  71. # Array.each iterates through each element of the array, enacts on that element what you have specified in the block
  72. # and then moves on to the next element. When the method moves to the next element in the array, the element that has
  73. # just been changed reverts to its original state. At the end, Array.each returns the original array. You have to store
  74. # the changes in another location if you want to use them.
  75. #
  76. # Array.map iterates through each element of the array, and enacts on each element what you have specified in the block.
  77. # It changes each element of the array and returns a changed array. However, the element is not permanently changed, so
  78. # if you want to use the changes elsewhere, you must store that information somewhere.
  79. #
  80. # Array.select iterates through each element of the array and enacts on that element what you have specified in the block.
  81. # If the block returns a truthy value, that element of the array is kept. If the block returns a falsy value, that element
  82. # of the array is temporarily deleted. Array.select returns an array of all the truthy values based on the block that is given.
  83. #
  84. # It is important to note (again) that each does not return a changed array. In thinking about each situation, let us think
  85. # about a computer keyboard. In the example, the keys of the keyboard represent the elements of an array. For Array.each
  86. # I write over one key in a temporary marker, changing that value, then wipe it off before changing the value of the next key.
  87. # At the end, when I hand you the keyboard, I will not have changed anything about it. For Array.map, I write over all the keys
  88. # on a keyboard, and give to you the whole keyboard. You do what you want to store that information, then I wipe off the keys
  89. # reverting all the keys back to their original state. For array.select, you give me a parameter (e.g. all consonants). If the
  90. # key returns a 'truthy' value, I pop that key off, place it on a blank keyboard. Then I hand you that new keyboard with all the
  91. # keys that match your parameter. After you do what you want to store the information, I return all the keys to the original keyboard
  92. # preserving its original state.
  93.  
  94.  
  95. #9
  96. # Method 1: Iterates through the array printing a string version of the element multiplied by 100.
  97. # Method 2: Sets the value of the first number of the array as variable "foo". Iterates through the array and compares each value to 'foo'.
  98. # If value is greater than 'foo', it changes that element's value to 'foo'. Returns the value 'foo' and the array is not changed.
  99. # Method 3: Creates an array and assigns it to the variable results. Iterates through the array, adding 80 to each number and shovels that value
  100. # Into results. Returns the array with the values+80. The original array is not modified.
  101. # Method 4: Creates an array and assigns it to variable results. Iterates through the array and shovels in only even numbers. Returns the array
  102. # results. The original array is not modified.
  103. #
  104. # Method 4 refactor:
  105.  
  106. def method_4(array)
  107. array.select { |num| num.even? }
  108. end
  109.  
  110. # 10
  111. # First off -> all the code has initialize spelled incorrectly.
  112. #
  113. # First example: You have need to initialize with one argument, but foo = Foo.new does not have that argument. Use Foo.new(argument) or do not
  114. # call for an argument in the initialize.
  115. # Second example: There is no getter method for the @num variable. It is inaccessible. Use attr_reader :num, or def num; @num; end.
  116. # Third example: There is no setter method for the @num variable. Use attr_writer :num, or change the attr_reader to attr_accessible, or use def num=(new_num); @num = new_num; end
  117.  
  118. # 11
  119. # The code will print...
  120. #
  121. # 'woof'
  122. # nil
  123. # 'meow'
  124. # nil
  125. # nil
  126. #
  127. # The first and third calls are actual keys in the hash. Because of the .inspect, the console will print the literal format of the value that coincides
  128. # with the key. The value of hash[:dog] is the string 'woof' and the value of hash['cat'] is 'meow'. The second, fourth and fifth calls ask for non-existent
  129. # keys, which will return nil.
  130.  
  131. # 12
  132. # Puts, which stands for "Put String" simply takes whatever is being.. puts-ed, transforms it into a readable version and displays it on the console. The information
  133. # that is being puts-ed is not stored anywhere. Return actually returns the value back to memory to be used.
  134. #
  135. # To use a simple analogy, imagine writing a word document. Say you write an essay. Puts would be equivalent to printing the essay and then closing the document without
  136. # saving it. Return is essentially hitting Save As.. You can store somewhere in memory where that value can be accessed, reused, and manipulated. Return does not automatically
  137. # save the variable somewhere. You must tell the program where to store that variable.
  138.  
  139. # 13
  140. # Fib fast ran in 4.3e-.05 seconds. Fib slow... is still running. Just kidding. Fib slow ran in 42.83 seconds. Fib_slow, while recursive, is creating
  141. # 2 branches at each level of recursion, while Fib fast is working off a single branch. And not to get ahead of myself, and please correct me on
  142. # this, but I think Fib_slow is running in 2^n time, while Fib_fast is running in n time.
Add Comment
Please, Sign In to add comment