LoginSignup
11
0

More than 3 years have passed since last update.

【Week 12-13】thor, robocop

Last updated at Posted at 2020-12-28

はじめに

マルチスケールシミュレーション特論の講義メモです.講義メモのインデックスはコチラ

今回の参考資料はチャート式ruby-appendix-VI(thor, rubocop)です.

チャート式 Ruby

今回は Ruby 開発周辺情報のみです.

Ruby 開発周辺情報

参照記事はコチラ

thor

thor ときいたら MARVELSTUDIO の『マイティ・ソー』をイメージしてしまう.トーアと呼ぶらしい.thor はコマンドラインツールを作る際に用いる gem で,rake と似たような立ち位置にいる.

まず

gem install thor

で thor をインストールする.

以前に hello_rudy を作成したのでそれらを流用する.講義内で環境設定を行っていたが,私は既に【Week 7】gem, ruby_thirdで済ませていたのでそのまま進めていく.

./hello_rudy/lib/hello_rudy.rb を以下のように編集する.

require "thor"
require "hello_rudy/version"

class HelloRudyCLI < Thor
  desc "hello NAME", "say hello to NAME"
  def hello(name)
    puts "Hello " + name
  end
  # class Error < StandardError; end
  # # Your code goes here...
  # puts "Hello #{ARGV[0]}."
end

HelloRudyCLI.start(ARGV)

実行しようとしてみると

> bundle exec exe/hello_rudy
bundler: failed to load command: exe/hello_rudy (exe/hello_rudy)
LoadError: cannot load such file -- thor

エラーが出力される.どうやら先ほどインストールした thor が見つからない thor だ.

これは .gemspec に追記してインストールすることで解決できる.

> emacs hello_rudy.gemspec
  spec.add_runtime_dependency('thor') を追記

> bundle update
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies...
Using rake 12.3.3
Following files may not be writable, so sudo is needed:
  /Library/Ruby/Gems/2.6.0
  /Library/Ruby/Gems/2.6.0/bin
  /Library/Ruby/Gems/2.6.0/build_info
  /Library/Ruby/Gems/2.6.0/cache
  /Library/Ruby/Gems/2.6.0/doc
  /Library/Ruby/Gems/2.6.0/extensions
  /Library/Ruby/Gems/2.6.0/gems
  /Library/Ruby/Gems/2.6.0/specifications
Using bundler 2.1.4
Using thor 1.0.1
Using hello_rudy 0.1.0 from source at `.`

Using minitest 5.14.2
Bundle updated!

> bundle install
Using rake 12.3.3
Following files may not be writable, so sudo is needed:
  /Library/Ruby/Gems/2.6.0
  /Library/Ruby/Gems/2.6.0/bin
  /Library/Ruby/Gems/2.6.0/build_info
  /Library/Ruby/Gems/2.6.0/cache
  /Library/Ruby/Gems/2.6.0/doc
  /Library/Ruby/Gems/2.6.0/extensions
  /Library/Ruby/Gems/2.6.0/gems
  /Library/Ruby/Gems/2.6.0/specifications
Using bundler 2.1.4
Using thor 1.0.1
Using hello_rudy 0.1.0 from source at `.`

Using minitest 5.14.2
Bundle complete! 3 Gemfile dependencies, 5 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.~

それではもう一度.実行できるだろうか.

> bundle exec exe/hello_rudy
Commands:
  hello_rudy hello NAME      # say hello to NAME
  hello_rudy help [COMMAND]  # Describe available commands or one specific command

> bundle exec exe/hello_rudy hello Rudy
Hello Rudy

問題なく実行できた.久しぶりに Rudy 登場.

続いて,実行できるようにインストールしてみる.

> sudo rake install:local
hello_rudy 0.1.0 built to pkg/hello_rudy-0.1.0.gem.
hello_rudy (0.1.0) installed.

> hello_rudy hello kitty
Hello kitty

無事サンリオのキャラクター名が出力された.

rake と似たような感じだが違いをあまり感じられなかった.後で調べてみる.

rubocop

またまた洋画名っぽいやつ.thor と rubocop で闘うつもりなのだろうか.

そんなことはおいておいて,rubocop とは code formatter の一種らしい.コードが規約どおり書かれているかチェックしてくれる.

フォーマット機能以外にも長いコードやったら怒られる機能もあるらしい.ついに PC にも怒られる時代が来たのか.

とりあえず気になったので使ってみる.まずはインストール.

sudo gem install rubocop                                                                                 333ms  Mon Dec 28 18:23:59 2020
Fetching parallel-1.20.1.gem
Fetching rubocop-ast-1.3.0.gem
Fetching ast-2.4.1.gem
Fetching regexp_parser-2.0.3.gem
Fetching parser-3.0.0.0.gem
Fetching ruby-progressbar-1.10.1.gem
Fetching rubocop-1.7.0.gem
Fetching unicode-display_width-1.7.0.gem
Successfully installed parallel-1.20.1
Successfully installed ast-2.4.1
Successfully installed parser-3.0.0.0
Successfully installed regexp_parser-2.0.3
Successfully installed rubocop-ast-1.3.0
Successfully installed ruby-progressbar-1.10.1
Successfully installed unicode-display_width-1.7.0
Successfully installed rubocop-1.7.0
Parsing documentation for parallel-1.20.1
Installing ri documentation for parallel-1.20.1
Parsing documentation for ast-2.4.1
Installing ri documentation for ast-2.4.1
Parsing documentation for parser-3.0.0.0
Installing ri documentation for parser-3.0.0.0
Parsing documentation for regexp_parser-2.0.3
Installing ri documentation for regexp_parser-2.0.3
Parsing documentation for rubocop-ast-1.3.0
Installing ri documentation for rubocop-ast-1.3.0
Parsing documentation for ruby-progressbar-1.10.1
Installing ri documentation for ruby-progressbar-1.10.1
Parsing documentation for unicode-display_width-1.7.0
Installing ri documentation for unicode-display_width-1.7.0
Parsing documentation for rubocop-1.7.0
Installing ri documentation for rubocop-1.7.0
Done installing documentation for parallel, ast, parser, regexp_parser, rubocop-ast, ruby-progressbar, unicode-display_width, rubocop after 22 seconds
8 gems installed

めっちゃ parser 入れられたやん.なんだか嫌な予感.

とりあえず実行してみた.

/SpaceAroundOperators: Surrounding space missing for operator ==.
  return 0 if n==0
               ^^
fibonacci_copy.rb:5:3: C: [Corrected] Layout/EmptyLineAfterGuardClause: Add empty line after guard clause.
  return 1 if n==1
  ^^^^^^^^^^^^^^^^
fibonacci_copy.rb:5:16: C: [Corrected] Layout/SpaceAroundOperators: Surrounding space missing for operator ==.
  return 1 if n==1
               ^^
fibonacci_copy.rb:6:3: C: [Corrected] Style/RedundantReturn: Redundant return detected.
  return fib(n-1) + fib(n-2)
  ^^^^^^
fibonacci_copy.rb:6:15: C: [Corrected] Layout/SpaceAroundOperators: Surrounding space missing for operator -.
  return fib(n-1) + fib(n-2)
              ^
fibonacci_copy.rb:6:26: C: [Corrected] Layout/SpaceAroundOperators: Surrounding space missing for operator -.
  return fib(n-1) + fib(n-2)
                         ^
fibonacci_copy.rb:9:4: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
   ^
fibonacci_copy.rb:9:7: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
      ^
fibonacci_copy.rb:9:10: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
         ^
fibonacci_copy.rb:9:13: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
            ^
fibonacci_copy.rb:9:16: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
               ^
fibonacci_copy.rb:9:19: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
                  ^
fibonacci_copy.rb:9:22: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
                     ^
fibonacci_copy.rb:9:25: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
                        ^
fibonacci_copy.rb:9:28: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
                           ^
fibonacci_copy.rb:9:31: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
                              ^
fibonacci_copy.rb:9:34: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
                                 ^
fibonacci_copy.rb:9:37: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
                                    ^
fibonacci_copy.rb:9:40: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
                                       ^
fibonacci_copy.rb:9:43: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
                                          ^
fibonacci_copy.rb:9:46: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
                                             ^
fibonacci_copy.rb:9:50: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
                                                 ^
fibonacci_copy.rb:9:53: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
[[0,0],[1,1],[2,1],[3,2],[4,3],[5,5],[6,8],[7,13],[8,21]].each do |index, expected|
                                                    ^
fibonacci_copy.rb:10:1: C: [Corrected] Layout/IndentationWidth: Use 2 (not 4) spaces for indentation.
    puts assert_equal(expected,fib(index))
^^^^
fibonacci_copy.rb:10:31: C: [Corrected] Layout/SpaceAfterComma: Space missing after comma.
    puts assert_equal(expected,fib(index))
                              ^

1 file inspected, 29 offenses detected, 26 offenses corrected, 2 more offenses can be corrected with `rubocop -A`~

嫌な予感的中.すごい怒られた.もう一生使わんとこ.

まとめ

講義の記事はこれにて終了.ruby は rails でアプリケーション作るためだけのものやと思っていたけど,どうやら違うみたい.

はじめの環境構築は大変だったけどTDDやオブジェクト指向,Rake,それとemacs,org,github,qiita 色々学べたのでなんだかんだ楽しかった.コロナ禍のオンライン授業お疲れ様でした.


  • source ~/grad_members_20f/members/e79a93e5b7b1/posts/class/c12_20201216.org
11
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
11
0