コードを書く時の Tips(Ruby, Python, Git)
目次
Directory
Ruby
ディレクトリ | 説明 | メモ |
---|---|---|
bin/ | バイナリ実行可能ファイル | |
lib/ | ライブラリモジュール | ラッパーも置いておくと〇 |
spec/ | unit テスト | gem rspec |
Rakefile | rake コマンドで使えるタスク | make ファイルの Ruby version |
Gemfile | このアプリケーションに必要な gem の定義ファイル | bunder init で生成 |
Gemfile.lock | アプリケーションで使われる gem のバージョンを確認するためのリスト | bundle install で生成 |
.gitignore | Git に取り込みたくないファイルを指定するためのパターン | |
.editconfig | 言語ごとの tab 数などのエディタ設定 | |
README.md | アプリケーションの簡単な説明 |
Rails (参考: デフォルトのRailsフォルダ構造 - Qiita )
ディレクトリ | 説明 | メモ |
---|---|---|
app/ | モデル、ビュー、コントローラ、ヘルパーなどを含む主要なアプリケーションコード | |
app/assets | アプリケーションで使う CSS (Cascading Style Sheet)、JavaScript ファイル、画像などのアセット | |
bin/ | バイナリ実行可能ファイル | |
config/ | アプリケーションの設定 | |
db/ | データベース関連のファイル | |
doc/ | マニュアルなど、アプリケーションのドキュメン | |
lib/ | ライブラリモジュール | |
lib/assets | ライブラリで使う CSS (Cascading Style Sheet)、JavaScript ファイル、画像などのアセット | |
log/ | アプリケーションのログファイル | |
public/ | エラーページなど、一般(Web ブラウザなど)に直接公開するデータ | |
bin/rails | コード生成、コンソールの起動、ローカルの Web サーバの立ち上げなどで使う Rails スクリプト | |
spec/ | unit テスト | gem rspec |
test/ | アプリケーションのテスト | |
tmp/ | 一時ファイル | |
vendor/ | サードパーティのプラグインや gem など | |
vendor/assets | サードパーティのプラグインや gem で使う CSS (Cascading Style Sheet)、JavaScript ファイル、画像などのアセット | |
README.md | アプリケーションの簡単な説明 | |
Rakefile | rake コマンドで使えるタスク | |
Gemfile | このアプリケーションに必要な gem の定義ファイル | |
Gemfile.lock | アプリケーションで使われる gem のバージョンを確認するためのリスト | |
config.ru | Rack ミドルウェア用の設定ファイル | |
.gitignore | Git に取り込みたくないファイルを指定するためのパターン | |
.rubocop | コーディング規約のチェック | gem rubocop, rubocop-rails |
Python
ディレクトリ | 説明 | メモ |
---|---|---|
bin/ | バイナリ実行可能ファイル | |
lib/ | module | moduleを呼び出すラッパーも置いておく |
tests/ | test code | |
Pipfile | このアプリケーションに必要な pip の定義ファイル | pipenv を使う場合 |
.gitignore | Git に取り込みたくないファイルを指定するためのパターン | |
.editconfig | 言語ごとの tab 数などのエディタ設定 | |
README.md | アプリケーションの簡単な説明 |
package
Ruby
パッケージ名 | 説明 | メモ |
---|---|---|
rbenv | ruby version 管理 | |
bundler | gem 管理 | |
rspec | unit テストの gem | bundler でインストール |
rails | Rails App を作成 | bundler でインストール |
Python
パッケージ名 | 説明 | メモ |
---|---|---|
pyenv | python version 管理 | |
pipenv | pip 管理 | poetry の選択肢もあり |
unittest | unit テストの pip | pipenv でインストール |
お作法
git commit
項目 | 参考 |
---|---|
git commit | Gitのコミットメッセージの書き方 - Qiita |
.gitignore
項目 | 参考 |
---|---|
.gitignore | https://github.com/github/gitignore |
.editconfig
項目 | 参考 |
---|---|
.editconfig | https://editorconfig.org/ |
Git(参考: gitコマンド チートシート - Qiita)
設定
用途 | コマンド |
---|---|
ユーザ名設定 | git config --global user.name "[name]" |
メールアドレス登録 | git config --global user.email "[example@example.com]" |
リポジトリ作成
用途 | コマンド |
---|---|
ローカルリポジトリ作成 | git init |
リモートからクローン作成 | git clone [url] |
確認
用途 | コマンド |
---|---|
staged / modified / untracked の一覧 | git status |
ローカルブランチの一覧 | git branch |
ローカルブランチとリモート追跡ブランチの一覧 | git branch -a |
[履歴] 現在のブランチの履歴 | git log |
[履歴] ファイルの履歴 | git log --follow [file/path] |
[差分] ブランチ間差分表示 | git diff [first-brach] [second-branch] |
[差分] ワーキングディレクトリ と ステージングエリア の差分を表示 | git diff |
ステージング
用途 | コマンド |
---|---|
指定のファイルを staged にする(スペース区切りで複数選択) | git add [file/path] |
すべての modified を staged にする (--update) | git add -u |
すべての untracked とすべての modified を staged にする (--all) | git add -A |
カレントディレクトリのすべてのファイルを staged にする (cf. -A はリポジトリ全体) | git add . |
staged から modified にもどす | git reset [file/path] |
コミット
用途 | コマンド |
---|---|
コミット | git commit -m "[comment]" |
ユーザ名メールアドレスを指定してコミット | git -c user.name='username' -c user.email='example@example.com' commit -m "[comment]" |
ステージング(untrackedは対象外) & コミット | git commit -a -m "[comment]" |
直前のコミットのコメントを修正する | git commit --amend -m "[comment]" |
チェックアウト
用途 | コマンド |
---|---|
チェックアウト | git checkout [branchname] |
ブランチ作成 & チェックアウト | git checkout -b [branchname] |
特定のコミットからブランチを作成してチェックアウト | git checkout -b [branchname] [コミットID] |
ブランチ
用途 | コマンド |
---|---|
ブランチ作成 | git branch [branchname] |
ブランチ名の変更 | git branch -m [new_branchname] |
ブランチ削除 | git branch -d [branchname] |
リモートブランチの削除 | git push --delete origin [remote_branchname] |
マージ (ブランチの合流) | git checkout [取込先ブランチ] git merge [対象ブランチ] --no-ff |
リベース (ブランチの付替え) | git checkout [対象ブランチ] git rebase [ブランチを生やすブランチ名] |
他ブランチのコミットを取り込む | git cherry-pick [コミットID] |
タグの作成 | git tag [tagname] |
リモートリポジトリ
用途 | コマンド |
---|---|
リモートリポジトリの一覧表示 | git remote |
リモートリポジトリのすべてのブランチの更新履歴をリモート追跡ブランチに取り込む | git fetch |
リモート追跡ブランチを指定ローカルブランチにマージする | git merge [origin/branchname] [branchname] |
リモートブランチの変更をローカルブランチに反映させる・1 | git fetch git checkout [branchname] git merge [origin/branchname] |
リモートブランチの変更をローカルブランチに反映させる・2 | git pull |
リモートブランチからローカルブランチの作成 | git branch [branchname] [remote_branchname] |
現在のブランチの変更をリモートブランチに反映させる | git push |
現在のブランチのリモートブランチを作成する | git push --set-upstream origin [branchname] |
ファイル編集・削除
用途 | コマンド |
---|---|
ファイル名の変更、ファイルの移動 | git rm [old_file/path] [new_file/path] |
ファイルを削除する | git rm [file/path] |
ファイルを監視対象から外す | git rm --cached [file/path] |
ディレクトリを監視対象から外す | git rm --cached -r [path] |
ファイル名変更・コミット | git mv [file_original] [file_renamed] |
変更の一時保存
用途 | コマンド |
---|---|
modified と staged を保存し、HEADの状態までクリーンに戻す | git stash |
直近の記録をワーキングディレクトリに反映する (LIFO(後入れ先出し)) | git stash pop |
スタッシュの一覧を表示する | git stash list |
直近の記録を破棄する (LIFO) | git stash drop |
やり直す
用途 | コマンド | 備考 |
---|---|---|
直前のコミットのコメントを変更する | git commit --amend -m "[comment]" |
|
commit_id 以降のすべてのコミットを取り消し (ワークツリーに残す) | git reset --soft [commit_id] |
[commit_id]をHEAD^ に変更すると直前のコミットを取り消す(ファイルは残る) |
commit_id 以降のすべてのコミットを取り消し (ワークツリーに残さない) | git reset --hard [commit_id] |
[commit_id]をHEAD^ に変更すると直前のコミットを取り消す(ファイルも消える) |
マージを取り消す | git merge --abort |
|
コミットを打ち消すコミットをする | git revert [commit_id] |
|
特定のファイルだけ以前の状態に戻す | git checkout [commit_id] [file/path] |
|
直前のコミットに変更を追加する | git add [file/path] git commit --amend --no-edit |
|
遡ってコメントを修正する | git rebase -i HEAD~3 # viエディタで対象のコミットのpickをeditに変更し、保存 git commit --amend -m "[comment]" git rebase --continue |
|
削除したブランチを戻す | git reflog # viでHEADの履歴が表示される # 消してしまったブランチの最後のコミットを見つけたらコミット番号をコピー git branch [branchname] [コミット番号] |
rails チートシート
bundler
Command | Description | memo |
---|---|---|
bundler -v | bundler バージョン確認 | |
bundle init | Gemfile 作成 | |
bundle install | Gemfile から gem をインストール (グローバル) | |
bundle install --path vendor/bundle | Gemfile から gem をインストール(ディレクトリ) | 非推奨 |
bundle config set path 'vendor/bundle' | bundle install 時のインストール先設定 | 実施後に bundle install で --path vendor/bundle 相当 |
bundle list | bundler でインストールした gemリスト一覧 | |
bundle exec [commands] | bundler を利用してプログラムを実行 | bundler を使っている場合は必須 |
Rails (bundler 利用前提)
Command | Description | memo |
---|---|---|
bundle exec rails new [Directory] | 指定したディレクトリに Rails app を作成 | |
bundle exec rails new [Directory] --skip-bundle | 指定したディレクトリに Rails app を作成(余分な bundle install が走らない) | 事後で bundle update する必要あり |
bundle exec rails server | Rails app を起動 | bundle exec rails s でも可デフォルト localhost:3000 |
bundle exec rails s | Rails app を起動(rails server の省略形) | |
bundle exec rails generate controller [Action_1] [Action_2] .. | Controller とそれに関連した view / html.erb の作成 | bundle exec rails g でも可 |
rails tutorial memo (MVCについて)
前回までの記事
- rails tutorial memo (rails 開発環境構築) - ニートのMEMO
- rails tutorial memo (Gemfile書き方) - ニートのMEMO
- rails tutorial memo (rails server) - ニートのMEMO
MVC とは
- Model-View-Controller のこと
- Rails application 内の app ディレクトリにある 「models」「views」「controllers」で管理
- Model は Controller が要求した情報を元に必要なデータを DB から取得する
- View は Controller が要求した情報を元に View を構築する
- Controller は ブラウザから来た要求に対して、Model や View に処理を振り分け中継する
- MVC では画面表示と内部のアプリケーションを別のコードで処理している
- ① ブラウザから Webサーバに Request が来ると Controller が中継してすぐに View を構築して html をブラウザに返す場合
- ② ブラウザから動的サイトに Request が来ると View を構築する前に Model がユーザ情報などを DB から取得してから Controller が View の構築を行い html をブラウザに返す場合
Controller
- 新規で Controller を作成していない場合は、
app/controllers/application_controller.rb
(application コントローラ)のみが存在する - 上記のファイルに処理を記載することで画面の表示などの処理を変更できる
# Hello World! を画面に表示するだけの Controller class ApplicationController < ActionController::Base def hello render html: "Hello World!" end end
- Hello Wold 用のアクションを定義したが、デフォルトだと上記の Controller が参照されないため、ルーティングをする必要がある
Router
- Router はブラウザと Controller の間に配置し、ブラウザからの Request を Controller に振り分ける役割
- ルーティングを変更する場合は、
config/routes.rb
に記載する
Rails.application.routes.draw do # For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html root 'application#hello' end
- 上記の
root 'application#hello'
では application コントローラ の hello アクションを呼び出すというルーティングをしている