singleton method就是外掛方法。簡體翻譯叫做單件方法。
適合這個方法用一次或不常用。
好處是不會污染原本類別(例:String類別)裡面的方法,避免Monkey Patch(猴子補丁)或叫MP。
舉例:
my_string = "I'm William."
def my_string.to_hello
"Hello," + self
end
my_string.to_hello
=> "Hello,I'm william."
Willy程式筆記
2020年3月3日 星期二
2020年2月12日 星期三
2017年7月24日 星期一
rails select data format
irb(main):015:0> puts User.last.attributes.to_yaml
id: 38
employee_number: '9900714'
name: 秦延凯
mobile: ''
password_digest: "$2a$10$H/Zb3HV3t6vubvVq1EPXEOQ8aGkqhBha7ZzZyBjotnnvMkTH2Vf5i"
created_at: 2017-05-11 06:30:35.018380000 Z
updated_at: 2017-05-11 06:30:35.018380000 Z
admin: false
id: 38
employee_number: '9900714'
name: 秦延凯
mobile: ''
password_digest: "$2a$10$H/Zb3HV3t6vubvVq1EPXEOQ8aGkqhBha7ZzZyBjotnnvMkTH2Vf5i"
created_at: 2017-05-11 06:30:35.018380000 Z
updated_at: 2017-05-11 06:30:35.018380000 Z
admin: false
2017年7月14日 星期五
rails array字串轉數字
["1", "2", "3"].map(&:to_i) => [1, 2, 3]
%w[1 2 3].map(&:to_i) => [1, 2, 3]
%w替代"x"
2017年7月13日 星期四
rails 撈數據庫轉array數組
User.all.map(&:id) => [36, 37, 1, 2, 4, 6, 7, 8, 9, 10, 11, 14, 13, 15, 17, 18, 16, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 38, 12]
User.all.map(&:id).class => Array
User.all.map(&:id).class => Array
rails map
哈希轉Array陣列
hash = { a:1, b:2, c:3 }
hash.map{|k,v| v} => [1, 2, 3]
哈希數組
hash = { :a => 1, :b => 2, :c => 3 } <--覺得打字不順手
因为符号(Symbol)做為key的情况太普遍了,Ruby 1.9 干脆为这种用法定义了一种新句法:
也可以這樣寫
hash = { a:1, b:2, c:3 }
k是key
v是value
在每個value裡面乘以2
hash.merge(hash) {|k,v| v*2 } => {:a=>2, :b=>4, :c=>6}
hash = { a:1, b:2, c:3 }
hash.map{|k,v| v} => [1, 2, 3]
哈希數組
hash = { :a => 1, :b => 2, :c => 3 } <--覺得打字不順手
因为符号(Symbol)做為key的情况太普遍了,Ruby 1.9 干脆为这种用法定义了一种新句法:
也可以這樣寫
hash = { a:1, b:2, c:3 }
k是key
v是value
在每個value裡面乘以2
hash.merge(hash) {|k,v| v*2 } => {:a=>2, :b=>4, :c=>6}
rails array移除掉nil
數組類型為Array
["a",nil,"b"].class => Array
移除掉空值
["a",nil,"b"].compact => ["a", "b"]
沒有nil需要移除掉返回nil
["a","b"].compact! => nil
2016年3月19日 星期六
open file vim plugin NERDTree
What's the "NERDTree"?
It's open file plugin by VIM.
http://www.flickr.com/photos/30496122@N07/2862367534/sizes/o/
How to install NERDTree
=========Create Folder============
cd ~
mkdir .vim
mkdir -p ~/.vim/autoload ~/.vim/bundle #Create Folder "autoload" and "bundle"
=========Load files===============
cd ~/.vim/bundle
git clone https://github.com/scrooloose/nerdtree.git
cd ~/.vim/autoload
curl -O https://raw.githubusercontent.com/tpope/vim-pathogen/master/autoload/pathogen.vim
=========Setting .vimrc============(If you don't have .vimrc...touch .vimrc)
execute pathogen#infect()
syntax on
filetype plugin indent on
=========Open NERDTree===========
Open VIM and type ":NERDTree"
or
vim ~/.vimrc
nnoremap <silent> <F5> :NERDTree<CR>
push F5 open NERDTree
Reference:
http://www.vim.org/scripts/script.php?script_id=1658
https://github.com/scrooloose/nerdtree
https://github.com/tpope/vim-pathogen
https://www.youtube.com/watch?v=-GdaZ-Fuk9w
It's open file plugin by VIM.
http://www.flickr.com/photos/30496122@N07/2862367534/sizes/o/
How to install NERDTree
=========Create Folder============
cd ~
mkdir .vim
mkdir -p ~/.vim/autoload ~/.vim/bundle #Create Folder "autoload" and "bundle"
=========Load files===============
cd ~/.vim/bundle
git clone https://github.com/scrooloose/nerdtree.git
cd ~/.vim/autoload
curl -O https://raw.githubusercontent.com/tpope/vim-pathogen/master/autoload/pathogen.vim
=========Setting .vimrc============(If you don't have .vimrc...touch .vimrc)
execute pathogen#infect()
syntax on
filetype plugin indent on
=========Open NERDTree===========
Open VIM and type ":NERDTree"
or
vim ~/.vimrc
nnoremap <silent> <F5> :NERDTree<CR>
push F5 open NERDTree
Reference:
http://www.vim.org/scripts/script.php?script_id=1658
https://github.com/scrooloose/nerdtree
https://github.com/tpope/vim-pathogen
https://www.youtube.com/watch?v=-GdaZ-Fuk9w
2016年1月29日 星期五
postgresql accept tcp/ip connections on port 5432
Modify two files and Restart PostgreSQL
1.pg_hba.conf
+ host all all 0.0.0.0/0 password
file path:
/etc/postgresql/9.4/main/pg_hba.conf
2.postgresql.conf
Remove the mark
#listen_addresses = '*'
#post = 5432
file path:
/etc/postgresql/9.4/main/postgresql.conf
Tyep Command Line:
service postgresql restart
2016年1月14日 星期四
Rails JSON API
可以用三種方式讓瀏覽器讀取URL顯示JSON
下面這種是撈取member這個table所有資料
http://localhost:3000/api/member
下面這種是撈取member的第一筆資料,如果要撈第50筆資料把數字改成50就好
http://localhost:3000/api/member/1
...
http://localhost:3000/api/member/50
下面這種跟http://localhost:3000/api/member/1 一樣都是撈第一筆資料只是URL寫法不同而已
http://localhost:3000/api/member/1.json
Model:
$rails g model member name:string email:string
Controller:
$rails g controller /api member
member_controller.rb
def index
@member = Member.all
render json: @member
end
def show
@member = Member.find(params[:id])
render json: @member
end
def create
@member = Member.new()
if @member.save
render json: @member
else
render json: @member.errors, status: :unprocessable_entity
end
end
def update
if@member.update()
render json: @member
else
render json: @member.errors, status: :unprocessable_entity
end
end
def destroy
@member.destroy
head :no_content
end
Routes:
namespace :api do
resource :members, only: [:index, :create, :show, :update, :destroy]
end
查詢資料
curl -i http://localhost:3000/api/member/
寫入資料
curl -i -X POST -H "Content-Type: application/json" -d '{"member": {"name":"Apple","email':"ABC@gmail.com"}}' http://localhost:3000/api/member
更新指定資料
curl -i -X PUT -H "Content-Type: application/json" -d '{"name":"Apple","email':"ABC@gmail.com"}}' http://localhost:3000/api/member/1
刪除指定資料
curl -i -X DELETE http://localhost:3000/api/member/1
下面這種是撈取member這個table所有資料
http://localhost:3000/api/member
下面這種是撈取member的第一筆資料,如果要撈第50筆資料把數字改成50就好
http://localhost:3000/api/member/1
...
http://localhost:3000/api/member/50
下面這種跟http://localhost:3000/api/member/1 一樣都是撈第一筆資料只是URL寫法不同而已
http://localhost:3000/api/member/1.json
Model:
$rails g model member name:string email:string
Controller:
$rails g controller /api member
member_controller.rb
def index
@member = Member.all
render json: @member
end
def show
@member = Member.find(params[:id])
render json: @member
end
def create
@member = Member.new()
if @member.save
render json: @member
else
render json: @member.errors, status: :unprocessable_entity
end
end
def update
if
end
def destroy
@member.destroy
head :no_content
end
Routes:
namespace :api do
resource :members, only: [:index, :create, :show, :update, :destroy]
end
查詢資料
curl -i http://localhost:3000/api/member/
寫入資料
curl -i -X POST -H "Content-Type: application/json" -d '{"member": {"name":"Apple","email':"ABC@gmail.com"}}' http://localhost:3000/api/member
更新指定資料
curl -i -X PUT -H "Content-Type: application/json" -d '{"name":"Apple","email':"ABC@gmail.com"}}' http://localhost:3000/api/member/1
刪除指定資料
curl -i -X DELETE http://localhost:3000/api/member/1
訂閱:
文章 (Atom)