Skip to content
This repository has been archived by the owner on May 17, 2022. It is now read-only.

Exceptions exercise #55

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
50 changes: 7 additions & 43 deletions environment.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,16 @@ In order to set yourself up for this course properly you are going to need a mac

## Setup your environment

You are going to need administrator access to your laptop to install the software that we are going to use. With the help of the group facilitator you can make the request.
ALl exercises should be done on your linux VDI. If you have don't have access to this contact someone who can help you.

After you have got administrator rights you are going to need to do a few bits and pieces:

* Register a Google Apps account to use Hangouts with
* Install Slack and get an invite to the Sky or Sky Training team from your facilitator (we use this for most communication)
* Install Google Chrome from the web (not Sky's self serve app store)
* Install the Google Hangouts Chrome extension (we'll use this for video calling in the event we're working remotely)
* Register for a Floobits account (Floobits is a really great service that will allow us to write code together)
* Register a Github account (if you don't already have one)
* Get access to Safari Books Online
* Get access to Pluralsight
* Get access to codeschool


## Install Ruby

To install Ruby we're going to use a tool called [`rbenv`](https://github.com/sstephenson/rbenv). To install this tool you can use [Homebrew](http://brew.sh/).

For more information about installing this tool, [consult the readme](https://github.com/sstephenson/rbenv#homebrew-on-mac-os-x).

The short version of this is:

```
# Install Homebrew

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

# Install rbenv

brew update
brew install rbenv ruby-build

# Install a Ruby version

rbenv install 2.2.3
```
You will need the following bits and pieces:

* Access to Safari Books Online
* Access to Pluralsight

## Checkout the bootcamp materials

The [ruby-bootcamp](https://github.com/sky-uk/ruby-bootcamp) repository is the starting point for the bootcamp. You'll want to [fork the repository](https://github.com/sky-uk/ruby-bootcamp) and clone it locally. Start by reading the [readme](https://github.com/sky-uk/ruby-bootcamp/blob/master/readme.md).
The [ruby-bootcamp](https://github.com/lvl-up/ruby-bootcamp-fil) repository is the starting point for the bootcamp. You'll want to clone it locally. Start by reading the [readme](https://github.com/lvl-up/ruby-bootcamp-fil/blob/master/readme.md).


## Setup Floobits

Floobits has a few features that require you to be authenticated against your account. Login in to Floobits and follow the instructions in under your settings area to create a .floorc.json in the home area of you VM.

This step is optional as you may not require Floobits to pair with your facilitator (as they might be in the same room).
## Install Ruby
From the terminal, cd in to where you have checked out the bootcamp repo and run `./setup.sh`
36 changes: 36 additions & 0 deletions exercises/exceptions/lib/cat.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
require_relative 'trick'

class Cat
attr_reader :tricks

# This constructor takes named parameters, If you've not seen them before then
# have a read. They can be pretty useful and in the right circumstances can
# make your code more readable
def initialize(age:, name:)
@name = name
@age = age
@tricks = {divide: Trick::DIVISION}
end


def age_in_human_years
if @age > 2
25 + calculate_remaining_years(@age - 2)
else
calculate_remaining_years(@age)
end
end

def do_trick(name, *args)
tricks[name].call(*args)
end

def print_age_in_human_years
puts "I'm #{age_in_human_years} human years old"
end

private
def calculate_remaining_years age
age * 4
end
end
15 changes: 15 additions & 0 deletions exercises/exceptions/lib/trick.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class Trick
attr_reader :trick
def initialize(&block)
@trick = block
end

def call(*args)
trick.call(*args)
end

DIVISION = Trick.new do |arg1, arg2|
arg1/arg2
end
end

52 changes: 52 additions & 0 deletions exercises/exceptions/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Exceptions


## Introduction

In this exercise you are going to write some exception handling code.

## Exercise

**_Note:_** You'll notice that there arn't any tests right now... Write them in order to add the new functionality required in the exercises that follow

### 1. Raise an exception
in [lib/cat.rb]('cat.rb') you'll find the `Cat` class. The constructor on this class requires you to set the age and name of the cat. However, this constructor could be more helpful than it is right now. Currently `Cat#initialize` will allow you to pass an age the is less than 0. This doesn't make much sense when thinking about cats and what's worse the `Cat#print_age_in_human_years` method just doesn't make sense when a negative age is used.

```RUBY
kitty = Cat.new(age: -1, name: 'billy')
kitty.print_age_in_human_years #=> "I'm -4 human years old"
```

### 2. Raise a specific Exception
Raising specific exceptions gives the user of your api the chance to make a choice about how they handle it.

Modify the code in the constructor to raise an ArguementError and a helpful message if the age specified is less than 0.

```RUBY
kitty = Cat.new(age: -1, name: 'billy') #=> ArgumentError"
```

### 3. Raise a Custom Error
Having custom exception types can be exceptionally useful (excuse the pun).

Define the method `Cat#learn_new_trick` that provides the signature utilised in the example below. If the cat is older than 100 in human years, throw a custom exception `ToBusySleepingError`

```RUBY
billy = Cat.new(name: 'billy', age: 2)
billy.learn_new_trick(:answer_the_phone) do |args|
#code that performs the trick
end

billy.tricks #=> [:answer_the_phone]

bob = Cat.new(name: 'bob', age: 35)
bob.learn_new_trick(:answer_the_phone) #> Raises ToBusySleepingError
```

### 4. Catch an Error
The cats built using the`Cat` are pretty clever. They know how to divide numbers! Problem is they don't know what to do when the second of those numbers is 0... Enhance the code for the skill to catch the divide by 0 error when if it happens and return nil instead.

```RUBY
billy = Cat.new(name: 'billy', age: 2)
billy.do_trick(:divide, 5,0) # => should return 0
```
2 changes: 1 addition & 1 deletion exercises/run-ruby/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ Use `irb` to run some of the statements in [runme.rb](runme.rb):

* What sort of object do you get when you run `Time.now`. (Use the `.class` method to find out)
* What other methods does that returned object have on it? (use the `.public_methods` method to find out, e.g. `Time.now.public_methods`)
* Use the Time RDoc to find out what extras you get
* Use the [Time RDoc](https://ruby-doc.org/stdlib-2.3.1/libdoc/time/rdoc/Time.html) to find out what extras you get
52 changes: 52 additions & 0 deletions setup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#!/bin/bash

PROFILELINE='export PATH=$PATH:/$HOME/.rvm/bin && [[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*'


PROFILEFILE=~/.profile
BASHRCLINE="source $PROFILEFILE"
BASHRCFILE=~/.bashrc
RUBYVERSION=2.3

checkerror () {
RC=$?
if [ $RC -ne 0 ]; then
echo "ERROR: $*"
fi
}

echo "Installing gpg key"
gpg --keyserver hkp://keys.gnupg.net --recv-keys 409B6B1796C275462A1703113804BB82D39DC0E3 7D2BAF1CF37B13E2069D6956105BD0E739499BDB
checkerror "Unable to install GPG key required to install RVM"

\curl -sSL https://raw.githubusercontent.com/wayneeseguin/rvm/stable/binscripts/rvm-installer | bash -s stable
checkerror "Unable io install RVM"

source $PROFILEFILE
checkerror "Unable to source profile file"

echo "Installing Ruby - enter corporate password when prompted"
rvm install ruby-$RUBYVERSION
checkerror "Unable to install Ruby version $RUBYVERSION"


grep -qF "$PROFILELINE" "$PROFILEFILE" || echo "$PROFILELINE" >> "$PROFILEFILE"
checkerror "Unable to add $PROFILELINE to $PROFILEFILE"

source $PROFILEFILE
checkerror "Unable to source profile file"

if [[ -f $BASHRCFILE ]]; then
grep -qF "$BASHRCLINE" "$BASHRCFILE" || echo "$BASHRCLINE" >> "$BASHRCFILE"
checkerror "Unable to add $BASHRCLINE to $BASHRCFILE"
else
echo "Unable to locate the bashrc file $BASHRCFILE"
exit 1
fi


source $PROFILEFILE
checkerror "Unable to source profile file"

rvm --default use $RUBYVERSION
checkerror "Unable to set the default version of ruby within RVM to $RUBYVERSION"