Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # 1
- def new_cat(str1, str2)
- str1 + str2
- end
- new_cat("Ken ", "Shimizu")
- # 2
- def sum(nums_to_add)
- nums_to_add.inject(0, :+)
- end
- # 3
- # "best" is at index 3.
- # "ruby" is at index 0.
- # The length is 7.
- # 4
- class String
- def fun_string!
- 0.upto(self.length-1) { |index| self[index] = self[index].capitalize! if index.odd? }
- self.reverse!
- end
- end
- # 5
- def fizzblam
- (1..100).each do |num|
- if num % 35 == 0 then puts "FizzBlam"
- elsif num % 5 == 0 then puts "Fizz"
- elsif num % 7 == 0 then puts "Blam"
- else puts num
- end
- end
- end
- # 6
- class GuessingGame
- def initialize(answer)
- @answer = answer
- @guess = nil
- end
- def guess(guess)
- @guess = guess
- if @guess > @answer then :high
- elsif @guess < @answer then :low
- else
- :correct
- end
- end
- def solved?
- @answer == @guess
- end
- end
- # 7
- # Global - Accessible by any class, instance, or method. Dangerous to use as any method or variable defined
- # in global scope can be easily overwritten
- # Class - Exists in the class itself. Instances generally have access to class scope, though some methods
- # defined in class scope are designated for only class access. E.g. Class methods (Array.new)
- # class variables
- # Instance - Exists within an instance of a class. E.g. Initializiation vars of an object used to keep
- # track of an instances traits. Can be accessed by methods within an instance but
- # not available outside the class without getters and setters.
- # Local - Exists only within the method or block that it is defined in. E.g. Create an array
- # within a method to shovel values into and then return at the end of the method.
- # 8
- # Array.each iterates through each element of the array, enacts on that element what you have specified in the block
- # and then moves on to the next element. When the method moves to the next element in the array, the element that has
- # just been changed reverts to its original state. At the end, Array.each returns the original array. You have to store
- # the changes in another location if you want to use them.
- #
- # Array.map iterates through each element of the array, and enacts on each element what you have specified in the block.
- # It changes each element of the array and returns a changed array. However, the element is not permanently changed, so
- # if you want to use the changes elsewhere, you must store that information somewhere.
- #
- # Array.select iterates through each element of the array and enacts on that element what you have specified in the block.
- # If the block returns a truthy value, that element of the array is kept. If the block returns a falsy value, that element
- # of the array is temporarily deleted. Array.select returns an array of all the truthy values based on the block that is given.
- #
- # It is important to note (again) that each does not return a changed array. In thinking about each situation, let us think
- # about a computer keyboard. In the example, the keys of the keyboard represent the elements of an array. For Array.each
- # I write over one key in a temporary marker, changing that value, then wipe it off before changing the value of the next key.
- # 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
- # 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
- # reverting all the keys back to their original state. For array.select, you give me a parameter (e.g. all consonants). If the
- # 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
- # keys that match your parameter. After you do what you want to store the information, I return all the keys to the original keyboard
- # preserving its original state.
- #9
- # Method 1: Iterates through the array printing a string version of the element multiplied by 100.
- # 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'.
- # If value is greater than 'foo', it changes that element's value to 'foo'. Returns the value 'foo' and the array is not changed.
- # 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
- # Into results. Returns the array with the values+80. The original array is not modified.
- # Method 4: Creates an array and assigns it to variable results. Iterates through the array and shovels in only even numbers. Returns the array
- # results. The original array is not modified.
- #
- # Method 4 refactor:
- def method_4(array)
- array.select { |num| num.even? }
- end
- # 10
- # First off -> all the code has initialize spelled incorrectly.
- #
- # 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
- # call for an argument in the initialize.
- # Second example: There is no getter method for the @num variable. It is inaccessible. Use attr_reader :num, or def num; @num; end.
- # 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
- # 11
- # The code will print...
- #
- # 'woof'
- # nil
- # 'meow'
- # nil
- # nil
- #
- # 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
- # 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
- # keys, which will return nil.
- # 12
- # 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
- # that is being puts-ed is not stored anywhere. Return actually returns the value back to memory to be used.
- #
- # 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
- # 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
- # save the variable somewhere. You must tell the program where to store that variable.
- # 13
- # 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
- # 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
- # 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