# Reflective RegressionRepository

### CA Bootcamp Week 1

Posted By Robin Börjesson on 2018-09-03

The first week of the Craft Academy Bootcamp is over. I have had a lot of fun. We started out with an ATM challenge in ruby. Over the weekend we did a library challenge in the same language. I feel like I have a good grasp of data management and manipulation. In MATLAB it was vectors and matrices, in ruby we have arrays and hashes.

Test Driven Development finally paid off. For the first time I could really see the benefit of it. It made me approach the problem in a more constructive manner and allowed me to think in directions which I wouldn’t do if I just went to work on the actual code.

The biggest take away from this week was the realization that I am really in my right element here. Coding is my calling, and if there was ever any doubt about my career change, it has been completely eliminated now.

### Yield, procs and lambdas

Posted By Robin Börjesson on 2018-08-20

You can pass blocks into a method. To make the block run you simply write the 'yield'-keyword:

``````def yield_name(name)
puts "In the method! Let's yield."
yield("Kim")
puts "In between the yields!"
yield(name)
puts "Block complete! Back in the method."
end

yield_name("Robin") { |n| puts "My name is #{n}." }
``````

A block can be saved in a variable using lambdas and procs. These are like light-weight methods that can be passed into actual methods just like blocks. When inside the method the proc or lambda is accessed by appending '.call'. or by yielding:

``````def greeter
yield
end

phrase = Proc.new {puts "Hello there!"}

greeter(&phrase)
``````

Notice how the calling of the proc is done by passing in the &-symbol before the proc name as a parameter to the greeter method. Lambas work the same way (notice the difference in instantiation):

``````strings = ["leonardo", "donatello", "raphael", "michaelangelo"]

symbolize = lambda {|i| i.to_sym}

symbols = strings.collect(&symbolize)
``````

The below example shows how they differ slightly. Lambdas will hand over control to the calling method while procs will not. Therefore, the first method will print out the proc call and then exit the method. The second method will instead print out the last line - meaning the lambda was run and then then it handed over control to the method again:

``````def batman_ironman_proc
victor = Proc.new { return "Batman will win!" }
victor.call
"Iron Man will win!"
end

puts batman_ironman_proc # => Batman will win

def batman_ironman_lambda
victor = lambda { return "Batman will win!" }
victor.call
"Iron Man will win!"
end
puts "----"
puts batman_ironman_lambda # => Ironman will win
``````

### Ruby - Require, Include and Extend

Posted By Robin Börjesson on 2018-08-20

The double colon syntax in ruby is something that I have seen quite a lot, but never really explored its under-pinnings. You see it quite a lot in the rails default setup, and I realised it had something to do with accessing libraries provided with the rails package. It turns out these are modules and the double colon is a way to access those modules if they are part of the interpreter. Example: Math is one such module and does not need to be 'required'. The namespace Math gets prepended using the double colon which enables access to all methods and constants that this module contains:

``````puts Math::PI
``````

If the module is not in the interpreter it can be brought in with the 'require' keyword:

``````require 'date'
puts Date.today
``````

Modules can also be 'included'. This gives the added benifit of not having to prepend the name of the module before accessing its funcitions and constants:

``````class Angle
include Math

end

def cosine
end
end

acute = Angle.new(1)
puts acute.cosine
``````

If we want the class itself to be able to use the methods we use the 'extend' keyword:

``````module ThePresent
def now
puts "It's #{Time.new.hour > 12 ? Time.new.hour - 12 :
Time.new.hour}:#{Time.new.min} #{Time.new.hour > 12 ?
'PM' : 'AM'} (GMT)."
end
end

class TheHereAnd
extend ThePresent
end

TheHereAnd.now
``````