ためすう
git のブランチ名を変更する
2019-10-10やったこと
ブランチ名を変更します
確認環境
$ git --version
git version 2.17.2 (Apple Git-113)
調査
パターン1 (oldbranch は省略)
変更前
$ git branch
master
* test1
$ git branch -m test2
$ git branch
master
* test2
test2
に変わりました。
パターン2 (oldbranch を指定)
変更前
$ git branch
* master
test2
$ git branch -m test2 test1
$ git branch
* master
test1
test2
-> test1
に変わりました。
ヘルプ抜粋
$ git branch --help
git branch (-m | -M) [<oldbranch>] <newbranch>
git add の取り消し (インデックスから削除する)
2019-10-09やったこと
git add
でインデックスに登録したあと、インデックスから削除します。
確認環境
$ git --version
git version 2.17.2 (Apple Git-113)
調査
git add でインデックスに登録する
$ git add app/controllers/users_controller.rb
$ git add ggg
ステータス確認
$ git --version
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: app/controllers/users_controller.rb
new file: ggg
$ git reset HEAD
Unstaged changes after reset:
M app/controllers/users_controller.rb
参考
git clean を使ってみる
2019-10-09やったこと
バージョン管理していないファイルを消します。
確認環境
$ git --version
git version 2.17.2 (Apple Git-113)
調査
ファイル準備
$ touch a
$ touch b
削除のドライラン
$ git clean -n
Would remove a
Would remove b
削除実行
$ git clean -f
Removing a
Removing b
less でオプション使ってみる
2019-10-09やったこと
less でログを見やすくするオプションを試しました。
調査
例えば、Rails のログをみるとします。
$ less log/development.log
Started GET "/" for ::1 at 2019-10-09 23:04:28 +0900
path is other
Processing by TasksController#index as HTML
ESC[1mESC[36mTask Load (1.3ms)ESC[0m ESC[1mESC[34mSELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT ?ESC[0m [["id", 1], ["LIMIT", 1]]
↳ app/controllers/tasks_controller.rb:7
Rendering tasks/index.html.erb within layouts/application
Rendered tasks/index.html.erb within layouts/application (1.8ms)
Completed 200 OK in 92ms (Views: 60.4ms | ActiveRecord: 1.8ms)
-qR オプションつき
$ less -qR log/development.log
Started GET "/" for ::1 at 2019-10-09 23:04:28 +0900
path is other
Processing by TasksController#index as HTML
Task Load (1.3ms) SELECT "tasks".* FROM "tasks" WHERE "tasks"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
↳ app/controllers/tasks_controller.rb:7
Rendering tasks/index.html.erb within layouts/application
Rendered tasks/index.html.erb within layouts/application (1.8ms)
Completed 200 OK in 92ms (Views: 60.4ms | ActiveRecord: 1.8ms)
おまけ
ESC という文字が消えています。
-q
は、ビープ音を鳴らさなくします。
-R
は、ANSI Escape sequences をカラー表示します。
$ man less
> -q or --quiet or --silent
> -R or --RAW-CONTROL-CHARS
正規表現でキャプチャを使う (Ruby)
2019-10-08やったこと
正規表現でキャプチャの結果を名前付きで取得します。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
調査
reg.rb
text = '2019年10月'
result = /(?<year>\d+)年(?<month>\d+)月/.match(text)
p result
p result[:year]
出力結果
$ ruby reg.rb
#<MatchData "2019年10月" year:"2019" month:"10">
"2019"
参考
dig を使ってみる (Ruby)
2019-10-08やったこと
dig を使ってみます。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
調査
dig.rb
a = [
{
k: 1,
v: 100
},
{
k: 2,
v: 200
}
]
p a.dig(0, :v)
p a.dig(1, :k)
p a.dig(2, :k)
出力結果
$ ruby dig.rb
100
2
nil
参考
Proc を使ってみる (Ruby)
2019-10-08やったこと
Proc を使ってみます。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
調査
ブロックをコンテキスト(ローカル変数のスコープやスタックフ レーム)とともにオブジェクト化した手続きオブジェクトです。
proc.rb
my_proc = Proc.new do |name|
'hoge ' + name
end
p my_proc.call('called')
def my_call(b)
p b.call('called in my_call')
end
my_call(my_proc)
出力結果
$ ruby proc.rb
"hoge called"
"hoge called in my_call"
参考
yield を使ってみる (Ruby)
2019-10-07やったこと
yield を使ってみます。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
調査
自分で定義したブロック付きメソッドでブロックを呼び出すときに使います。 yield に渡された値はブロック記法において | と | の間にはさまれた 変数(ブロックパラメータ)に代入されます。
yield.rb
def hoge
yield('hoge')
end
hoge {|name|
p name.upcase * 3
}
出力結果
$ ruby yield.rb
"HOGEHOGEHOGE"
参考
refine を使ってみる (Ruby)
2019-10-07やったこと
refine を使ってみます。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
調査
module-function.rb
class Sample
def hoge
"#{self.class.name}:#{__method__}"
end
end
module M
refine Sample do
def hoge2
"#{self.class.name}:#{__method__} 222"
end
end
end
p Sample.new.hoge
begin
p Sample.new.hoge2
rescue => e
p e
end
using M
p Sample.new.hoge2
出力結果
$ ruby refine.rb
"Sample:hoge"
#<NoMethodError: undefined method `hoge2' for #<Sample:0x00007fa29e0f1f48>
Did you mean? hoge>
"Sample:hoge2 222"
参考
module_function を使ってみる (Ruby)
2019-10-07やったこと
module_function を使ってみます。
確認環境
$ ruby --version
ruby 2.6.3p62 (2019-04-16 revision 67580) [x86_64-darwin17]
調査
module-function.rb
module Sample1
def hoge1
'hoge1'
end
module_function :hoge1
def hoge2
'hoge2'
end
end
class Sample2
include Sample1
end
p Sample1.hoge1
p Sample2.new.hoge2
p Sample2.new.hoge1
出力結果
$ ruby module-function.rb
"hoge1"
"hoge2"
Traceback (most recent call last):
module-function.rb:19:in `<main>': private method `hoge1' called for #<Sample2:0x00007fd48b8766c8> (NoMethodError)
Did you mean? hoge2