10
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Thor & Rubocop

Last updated at Posted at 2020-12-19

Document Links

Thor

Thorの基礎知識

ThorとはCLIの作成を支援するGemらしい.

詳しくは実際に扱いながら学ぶ.

また,有志の力を借りる

CLIを自作

まずはthorをinstallする.

$ sudo gem install thor

実際に作成していくが,

で勉強したbundlerはinstallしておいてくださいね.

いざ,実践!とその前に,今回の作業場は普段のgrad~/menber/[yours]では面倒らしいので(理由はわかりません),HomeDirectoryで行ってください.

$ pwd
/home/username #自分のホームディレクトリで作業開始!
$ bundle gem hello_rudy -b #gem hello_rudyが作成されます
$ cd hello_rudy
$ ls exe/ #exeファイルがあるかを確認
hello_rudy
$ chmod a+x exe/hello_rudy #権限を与えます

このままでは動かないので,実際に実行できるようにしていく.

$ emacs -nw hello_rudy.gemspec

で,中身を

require_relative 'lib/hello_rudy/version'

Gem::Specification.new do |spec|
  spec.name          = "hello_rudy"
  spec.version       = HelloRudy::VERSION
  spec.authors       = ["[yours]"]
  spec.email         = ["[yours]"]

  spec.summary       = %q{hello rudy}
  spec.description   = %q{hello rudy}
  spec.homepage      = "https://hoge.hoge"
  spec.license       = "MIT"
  spec.required_ruby_version = Gem::Requirement.new(">= 2.3.0")

#  spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"

  spec.metadata["homepage_uri"] = spec.homepage
#  spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
#  spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
  spec.files         = Dir.chdir(File.expand_path('..', __FILE__)) do
    `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
  end                                                                           
  spec.bindir        = "exe"
  spec.executables   = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
  spec.require_paths = ["lib"]

  spec.add_runtime_dependency('thor')
end 

こんな感じに修正してください.

$ bundle exec exe/hello_rudy hello rudy

これで何もErrorを出さなかったら大丈夫.Errorが出るなら,Error文を見て修正してください.

では次に,挙動の中身を書いていく.

$ emacs -nw lib/hello_rudy.rb

で,中身を

hello_rudy.rb
require "thor"
require "hello_rudy/version"

#module HelloRudy
#class Error < StandardError; end

class HelloRudyCLI < Thor
  desc "hello NAME", "say hello to NAME"
  def hello(name)
    puts "Hello " + name
  end
end

HelloRudyCLI.start(ARGV)

にしてください.

また,

$ emacs -nw lib/hello_rudy/version.rb

の中身を

version.rb
class HelloRudy
  VERSION = "0.1.0"
end

にしてください.

$ bundle update
$ bundle install
$ bundle exec exe/hello_rudy
Commands:
  hello_rudy hello NAME      # say hello to NAME
  hello_rudy help [COMMAND]  # Describe available commands or one specific co...
$ bundle exec exe/hello_rudy hello rudy
Hello Rudy

完成!

折角なので,どこからでも実行できるようにする.

$ sudo rake install:local
$ hello_rudy hello boy
Hello boy

これでどこからでもhello_rudyを実行できる.

Rubocop

Rubocopの基礎知識

コーディング規約に準拠しているかを調べるGemらしい.

こちらも有志の力を借りて勉強する.

実践

実際に使ってみる.

Install

$ sudo gem install rubocop

今回は–auto-correctとやらを使う.

$ rubocop --auto-correct file.rb

で,自動でCodeを整理してくれる.

どうなるかは自分の目で確かめてください.

締め

今回はThorとRubocopについて学んだ.


  • source ~/school/multi/my_ruby/grad_members_20f/members/evendemiaire/post/thor_rubocop.org
10
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
10
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?