- Create a fork of this repository.
- Make sure asdf is installed as well as ruby plugin
- In root directory of project, install ruby using
asdf install - Install project dependencies using
bundle install
-
Write a method named
with_these_numbersin theClosuresmodule that will operate on 2 numbers based on a given block. If no block is provided return a list of the numbersresult = Closures.with_these_numbers(1, 4) do |first, second| first + second end result # Output: 5 result = Closures.with_these_numbers(3, 6) do |first, second| first * second end result # Output: 18 result = Closures.with_these_numbers(12, 3) do |first, second| first / second end result # Output: 4 result = Closures.with_these_numbers(1,2) result # Output [1,2]
Run the specs using
rspec spec/closures_spec.rb. Make them pass. -
Create your own implementation of the
.mapmethod in theClosuresmodule. It takes in a list of items and a block then returns a new version of the list modified by what's in the block. If no block is provided return the original list. Do not use Ruby's built-in.map/.collectmethod.result = Closures.map([1,2,3]) do |item| item * 2 end result # Output: [2,4,6] result = Closures.map([1,2,3]) result # Output: [1,2,3]
Run the specs using
rspec spec/closures_spec.rb. Make them pass. -
Create your own implementation of the
.filtermethod in theClosuresmodule. It takes in a list of items and a block then returns a filtered version of the list. If no block is provided return the original list. Do not use Ruby's built-in.filter/.selectmethod.result = Closures.filter([1,2,3]) do |item| item.odd? end result # Output: [1,3] result = Closures.filter([1,2,3]) result # Output: [1,2,3]
Run the specs using
rspec spec/closures_spec.rb. Make them pass. -
Create a
Carclass that allows this syntax to workcar = Car.new(color: "Red", manufacturer: "Honda") do |c| c.plate_number = "ABC 1234" c.manufacturer = "Toyota" end car.color # Red car.plate_number # ABC 1234 car.manufacturer # Toyota
Run the specs using
rspec spec/car_spec.rb. Make them pass.
Create a class that uses this syntax
bootcamp = Bootcamp.new do
topic "Inheritance" do
description "All about inheritance"
details do
lecturer "Ady"
date "2023-3-28"
end
end
topic "Closures" do
description "Blocks, Procs and Lambdas"
details do
lecturer "Ady"
date "2023-3-28"
end
end
endThis should create a new bootcamp object with the declared items saved as topics with the following fields:
- title
- description
- lecturer
- date
Tip: instance_eval is a way to add a method to an instance of a class on the spot. It takes in either a string or block and executes that within scope of the instance (e.g. has access to variables and methods)
Run the specs using rspec spec/bootcamp_spec.rb. Make them pass.
- Open
irb - Import file to use in console e.g.
require "./closures.rb". - You should be able to use the class in the console.
Create a Pull Request directed to your fork of this repository then provide a link to the PR to the facilitators.