[RUBY] [Week 12-13] thor, robocop

Introduction

This is a lecture memo of the special lecture on multi-scale simulation. The index of lecture memos is here

The reference material this time is Chart type ruby-appendix-VI (thor, rubocop).

Chart type Ruby

This time, only the Ruby development peripheral information.

Ruby development peripheral information

Reference article is here

thor

When it comes to thor, I imagine MARVELSTUDIO's" Mighty Thor ". It seems to be called Toa. thor is a gem used to create command line tools and is in a position similar to rake.

First

gem install thor

Install thor with.

Since I created hello_rudy before, I will use them. I had set the environment in the lecture, but I have already done it with [Week 7] gem, ruby ​​\ _third, so I will proceed as it is.

Edit ./hello_rudy/lib/hello_rudy.rb as follows.

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)

When I try to run it

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

An error is output. Apparently, I can't find the thor I just installed.

This can be solved by adding it to .gemspec and installing it.

> emacs hello_rudy.gemspec

Added 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.~

Then again. Can it be done?

> 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

I was able to execute it without any problems. Rudy appeared after a long absence.

Next, try installing it so that it can be executed.

> 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

The character name of Sanrio was output safely.

It feels similar to rake, but I didn't notice much difference. I will check it later.

rubocop

Again, it looks like a Western movie name. Are you going to fight thor with rubocop?

Aside from that, rubocop seems to be a kind of code formatter. It will check if the code is written according to the rules.

In addition to the format function, there seems to be a function that gets angry if you do long code. Is the time when PCs are finally angry?

I was curious for the time being, so I will use it. First install.

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

It was really parsered. Somehow unpleasant premonition.

I tried it for the time being.

/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`~

Unpleasant premonition. I was very angry. I will use it for the rest of my life.

Summary

This is the end of the lecture article. ruby thought he was just for building applications with rails, but apparently not.

It was difficult to build the environment at the beginning, but it was fun because I learned TDD, object-oriented programming, Rake, and emacs, org, github, and qiita. Thank you for your hard work on the online class of Corona.


Recommended Posts

[Week 12-13] thor, robocop
Thor & Rubocop