HomebrewでupgradeしたらError: An unexpected error occurred during the `brew link` stepとなった時の対応方法

**環境**
OS : macOS Hight Sierra Virsion 10.13.1
Homebrew : １年ぐらい使っていなかった

## 事象 : brew upgradeしたら怒られた
```shell-session
$ brew upgrade
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 5 taps (homebrew/apache, homebrew/science, homebrew/core, homebrew/php, caskroom/cask).
==> New Formulae
abyss                                haste-client                         homebrew/php/php72-phalcon           metis

<超長いので省略>

==> Upgrading ruby 
==> Downloading https://homebrew.bintray.com/bottles/ruby-2.4.2_1.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring ruby-2.4.2_1.high_sierra.bottle.tar.gz
Error: An unexpected error occurred during the `brew link` step
The formula built, but is not symlinked into /usr/local
Permission denied @ dir_s_mkdir - /usr/local/lib/ruby/gems/2.4.0/doc/rubygems-2.6.14
Error: Permission denied @ dir_s_mkdir - /usr/local/lib/ruby/gems/2.4.0/doc/rubygems-2.6.14
```
## 原因 : `/usr/local`のPermissionがdeniedだから
```shell-session:doctorに聞いてみる
$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry and just ignore them. Thanks!

Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  ruby

Warning: Some installed formula are missing dependencies.
You should `brew install` the missing dependencies:
  brew install libtool

Run `brew missing` for more details.
```

```shell-session:doctorに言われたとおりにしてみる
$ sudo brew link ruby
Error: Running Homebrew as root is extremely dangerous and no longer supported.
As Homebrew does not drop privileges on installation you would be giving all
build scripts full access to your system.
```

```shell-session:なんか怖いこと言ってる気がするのでエラーに立ち戻る
Error: An unexpected error occurred during the `brew link` step
The formula built, but is not symlinked into /usr/local
Permission denied @ dir_s_mkdir - /usr/local/lib/ruby/gems/2.4.0/doc/rubygems-2.6.14
Error: Permission denied @ dir_s_mkdir - /usr/local/lib/ruby/gems/2.4.0/doc/rubygems-2.6.14
```

**Permissionがdeniedって言われているのに気がつく**

## 対応方法 : `/usr/local`のPermissionをdeniedじゃなくす

```shell-session:Permissionをdeniedじゃなくす
$ sudo chown -R $(whoami) /usr/local
```

```shell-session:再度アップグレードするとできる
$ brew upgrade
Updating Homebrew...
==> Auto-updated Homebrew!
Updated 1 tap (homebrew/core).
==> New Formulae
elasticsearch@5.6
==> Updated Formulae
elasticsearch

==> Upgrading 9 outdated packages, with result:
gmp 6.1.2_1, wget 1.19.2, libpng 1.6.34, freetype 2.8.1, mpfr 3.1.6, glassfish 5.0, homebrew/php/php56 5.6.32_8, gcc 7.2.0, automake 1.15.1
==> Upgrading automake 
==> Downloading https://homebrew.bintray.com/bottles/automake-1.15.1.high_sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring automake-1.15.1.high_sierra.bottle.tar.gz
🍺  /usr/local/Cellar/automake/1.15.1: 131 files, 3.0MB

<超長いので省略>

==> Upgrading gcc 
Warning: Building gcc from source:
  The bottle needs the Xcode CLT to be installed.
==> Downloading https://ftp.gnu.org/gnu/gcc/gcc-7.2.0/gcc-7.2.0.tar.xz
######################################################################## 100.0%^[[1;2C
==> Downloading https://raw.githubusercontent.com/Homebrew/formula-patches/e9e0ee09389a54cc4c8fe1c24ebca3cd765ed0ba/gcc/6.1.0-jit.patch
######################################################################## 100.0%
==> Downloading https://raw.githubusercontent.com/Homebrew/formula-patches/df0465c02a/gcc/apfs.patch
######################################################################## 100.0%
==> Patching
==> Applying 6.1.0-jit.patch
patching file gcc/jit/Make-lang.in
==> Applying apfs.patch
patching file libstdc++-v3/include/Makefile.in
==> ../configure --build=x86_64-apple-darwin17.2.0 --prefix=/usr/local/Cellar/gcc/7.2.0 --libdir=/usr/local/Cellar/gcc/7.2.0/lib/gcc/7 --enable-
==> make
```