2015年5月6日水曜日

gitでのコミットの打ち消し

gitで過去の変更を取り消したいときgit reset --hardは危険。コミットしてない変更があると完全に中身を消去してしまう。(これのせいで半日を無駄にした)
コミットログを汚して許容される環境ではgit revertで打ち消すべき
revertしただけだと、消したファイルはワーキングディレクトリから消えたままなのでそのcheckoutすべき

git revert <commit>
git checkout .

2015年5月5日火曜日

rubyでのファイル名に適したTimeの文字列表現

ファイルを作るスクリプトを何度も起動する必要がある場合いちいちファイル名を指定ルルのは面倒。 現在時刻から文字列を作成してファイル名にしておくといちいち指定する必要がなくて便利. しかしTime.now.to_s現在時刻の文字表現を取得すると、ファイル名にスペースが入って気持ちが悪い。 さらに、頭が年度だとタブでのファイル名保管が面倒

2.1.4 :013 > Time.now.to_s
 => "2015-05-06 00:20:39 +0900"

こんなかんじがよい

2.1.4 :008 > Time.now.strftime("m%M-h%H-d%d-%b-%Y")
=> "m53-h16-d06-May-2015"

2015年5月4日月曜日

RailsのMongoidでMongodbがtimeoutする

Mongodbはデフォルトで10分でカーソルがタイムアウトしてしまう
バッチ処理などの長時間の処理が途中で止まってしまう。

次のように書くと途中で止まってしまう。

Product.each{|product|
    # 何かの長時間かかる処理
}

10分を越える可能性のある処理は次のようにしましょう

Product.no_timeout.each{|product|
    # 何かの長時間かかる処理
}

2015年4月18日土曜日

CentOSでFTPサーバーを立てる(3分で)

インストール
sudo yum install vsftpd -y
起動
service vsftpd status
SELinuxを切る(重要)
setenforce 0
クライアント側から接続
$ ftp (ip_address)
ダウンロード
ftp> pwd
257 "/home/sato"
ftp> get images.tar.gz

2015年4月7日火曜日

カラフルで素晴らしいPS1シェルプロンプト

PS1シェルプロンプト

素晴らしいシェルプロンプトを作っている人が。。
gitのブランチをプロンプトにだすという素晴らしいアイディア
http://unix.stackexchange.com/a/178817

Bash $PS1 Generator2.0を使ってさらに好みにしてみた
https://www.kirsle.net/wizards/ps1.html

~/.bashrc以下を追加

git_branch () { git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/\1/'; }
# Custom bash prompt via kirsle.net/wizards/ps1.html
export PS1="\[$(tput bold)\]\[$(tput setaf 2)\]\t \[$(tput setaf 4)\]\u@\h\[$(tput setaf 1)\] \W \[$(tput setaf 3)\]\$(git_branch)\[$(tput sgr0)\] $ \[$(tput sgr0)\]"

gitないとき

# Custom bash prompt via kirsle.net/wizards/ps1.html
export PS1="\[$(tput bold)\]\[$(tput setaf 2)\]\t \[$(tput setaf 4)\]\u@\h\[$(tput setaf 1)\] \W \[$(tput sgr0)\] $ \[$(tput sgr0)\]"

完成!!

2015年4月3日金曜日

rubyの@とselfのちがい

下によると、@hoge='hoge'はインスタンス変数を直接書き換えてself.hoge='hoge'はセッターのメソッド経由で書き換えるらしい。

https://www.ruby-forum.com/topic/3546147

2015年3月3日火曜日

railsでのHTTP authentication

railsでのHTTP authentication

app/controllers/application_controller.rbに次のようにhttp_basic_authenticate_withを追加すればOK

class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception
  http_basic_authenticate_with name: "sato", password: "pass" #これを追加
end

こんな感じでURLでアクセス得きるので便利!

http://sato:pass@localhost:3000/