重點:
- 過去股票紅利是照面額計算 (Ex. 分紅10萬,相當10張股票,但是股票市值往往遠超過面額)
- 依據上一點,「面額」及「市值」之間的成本便由小股東承擔了,也造成每股盈餘不明的情況
- 費用化後,因員工配股張數是以「分紅金額」除以前一年最後交易日的股價
滿有趣的一點是:Node 團隊在前年左右放棄繼續遵守 CommonJS 的規範。Isaac 指出 "Ryan basically always gave zero fucks about CommonJS anyway" ,並轉述了 Ryan 的一句重話 : "Forget CommonJS. It's dead. We are server side JavaScript."
有興趣可以看看這個Github的討論串:https://github.com/joyent/node/issues/5132#issuecomment-15432598
( Issac 是 NPM 的作者, Ryan 是 Node 之父 )
Just the UI
Lots of people use React as the V in MVC. Since React makes no assumptions about the rest of your technology stack, it's easy to try it out on a small feature in an existing project.
<meta name=”viewport” content=”width=device-width, initial-scale=1, maximum-scale=1″>
def get_price(product_id):
...
return price
def christmas_discount(func):
discount = get_discount()
def get_christmas_price(product_id):
return func(product_id) * discount
return get_christmas_price
get_price(21) // ==> 200 get_price = christmas_discount(get_price) get_price(21) // ==> '160'
@christmas_discount
def get_price(product_id):
...
return price
get_price(21) // ==> '160'
註:如需從origin拉下一個branch,需在本地創一個branch,checkout 後再拉 "origin/branchname"。eg. "git pull origin test"
SELECT model FROM (
SELECT * FROM pc
UNION
SELECT * FROM laptop
UNION
SELECT * FROM printer
)
WHERE price=(
SELECT MAX(price) FROM (
SELECT * FROM pc
UNION
SELECT * FROM laptop
UNION
SELECT * FROM printer
)
)
WITH product AS ( SELECT * FROM pc UNION SELECT * FROM laptop UNION SELECT * FROM printer ) SELECT model FROM product WHERE price=( SELECT MAX(price) FROM product )
function * gen(){
console.log("start!");
yield "hello";
yield "I'm Kevin";
console.log("end!");
}
var g = gen(); // g 是一個 Generator
r = g.next(); // "start!"
console.log(r); // { value: "hello", done: false }
r = g.next();
console.log(r); // { value: "I'm Kevin", done: false }
r = g.next(); // "end!"
console.log(r); // { value: undefined, done: true }
function * gen(){
var got = yield "hello";
yield got;
}
var g = gen();
g.next("Good morning!"); // "Hello"
g.next(); // "Good morning!"
function foo() {
console.error('foo');
}
process.nextTick(foo);
console.error('bar');
bar foo
setTimeout(foo, 0);
console.log('bar');
config.assets.prefix = "/dev/assets"
PG::UniqueViolation: ERROR: Duplicate Key Value Violates Unique Constraint 'Your_table_name_pkey'
rails db productionSELECT setval('your_table_id_seq', (SELECT MAX(id) FROM your_table));config.i18n.fallbacks = true 。![]() |
| (轉自: ROR實戰聖經) |
user deployer; # 定義操作 nginx 的使用者
worker_processes 1; # 定義 worker 的數量
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
events {
worker_connections 1024;
}
http {
include /etc/nginx/mime.types;
default_type application/octet-stream;
access_log /var/log/nginx/access.log;
sendfile on;
tcp_nopush on;
tcp_nodelay on;
keepalive_timeout 65;
gzip on;
gzip_http_version 1.0;
gzip_comp_level 2;
gzip_proxied any;
gzip_vary off;
gzip_types text/plain text/css application/x-javascript text/xml application/xml application/rss+xml application/atom+xml text/javascript application/javascript application/json text/mathml;
gzip_min_length 1000;
gzip_disable "MSIE [1-6]\.";
server_names_hash_bucket_size 64;
types_hash_max_size 2048;
types_hash_bucket_size 64;
include /etc/nginx/conf.d/*.conf;
include /etc/nginx/sites-enabled/*;
}
upstream unicorn {
server 127.0.0.1:8080 fail_timeout=0;
}
server {
listen 80 default deferred;
server_name flytutor.com;
root /var/www/flytutor/public;
location ^~ /assets/ {
gzip_static on;
expires max;
add_header Cache-Control public;
}
location / {
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $http_host;
proxy_redirect off;
if (!-f $request_filename) {
proxy_pass http://unicorn;
}
}
error_page 500 502 503 504 /500.html;
client_max_body_size 4G;
keepalive_timeout 10;
}
upstream flytutor.com {
server 192.168.0.111:8080 weight=3;
server 192.168.0.222:8080 weight=2;
server 192.168.0.333:8080 weight=3;
}
其中 weight 代表的是「被配發 request 的權重」,當 weight 越高的時候被分配到的機率越大,可以做 loading balance。 upstream unicorn {
server unix:/tmp/unicorn.sock fail_timeout=0;
}
app_root = "/var/www" app_name = "flytutor" listen "127.0.0.1:10101" #, :backlog => 2048 #這邊要跟nginx虛擬主機檔中upstream內定義的務必一樣 worker_processes 2 #看情況開 preload_app false timeout 30 module Rails class <<self def root File.expand_path(__FILE__).split('/')[0..-3].join('/') end end end _working_directory = File.join(app_root, app_name) working_directory _working_directory logs_path = "#{_working_directory}/log" pid "#{_working_directory}/tmp/pids/unicorn.pid" stderr_path "#{logs_path}/unicorn.stderr.log" stdout_path "#{logs_path}/unicorn.stdout.log" GC.respond_to?(:copy_on_write_friendly=) and GC.copy_on_write_friendly = true before_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.connection.disconnect! old_pid = "#{Rails.root}/tmp/pids/unicorn.pid.oldbin" if File.exists?(old_pid) && server.pid != old_pid begin Process.kill("QUIT", File.read(old_pid).to_i) rescue Errno::ENOENT, Errno::ESRCH puts "Send 'QUIT' signal to unicorn error!" end end end after_fork do |server, worker| defined?(ActiveRecord::Base) and ActiveRecord::Base.establish_connection end
/usr/bin/unicorn_rails -c config/unicorn.rb -E $RAILS_ENV -D。但現在 unicorn 官方已經不建議使用這種作法而是直接改用 unicorn 來啟動/重啟 unicorn。$ gem install vagrant #安裝 vagrant $ vagrant box add ubuntu http://cloud-images.ubuntu.com/vagrant/quantal/current/quantal-server-cloudimg-i386-vagrant-disk1.box #安裝新的 Vagrant Package。這裡的 ubuntu 是一個預先做好的空的 ubuntu 12.10 (intel-based) $ vagrant init ubuntu $ vagrant upbox 檔可以在 http://www.vagrantbox.es/ 下載
config.vm.network :hostonly, "33.33.33.33" 將ip改成自己想要的ip,這裡以 "33.33.33.33" 為例。修改完後要執行 vangrant reload [default] Failed to connect to VM!當出現上面的錯誤訊息時,可以照 http://vagrant.wikia.com/wiki/Usage 上的步驟來排除,基本上就是將 Vagrantfile 中的
Failed to connect to VM via SSH. Please verify the VM successfully booted
by looking at the VirtualBox GUI.
config.vm.boot_mode = :gui 設定打開。再重新 vagrant up,在產生的 GUI 中輸入 sudo dhclient eth0。[default] The guest additions on this VM do not match the install version of
VirtualBox! This may cause things such as forwarded ports, shared
folders, and more to not work properly. If any of those things fail on
this machine, please update the guest additions and repackage the
box.
User.all.pluck :name # => ["Kevin", "Laura", "Yiya", "Diya"]Order.where( :product_id => Product.where("price<1000") )Model.where("state == 'decline'").update_all(:state => 'deny')has_many :cancled_orders, :class_name=>"Order", :conditions=>proc{ "status = 'Cancled'" }
has_many :handling_orders, :class_name=>"Order", :conditions=>proc{ "status = 'Handling'" }
has_many :paid_orders, :class_name=>"Order", :conditions=>proc{ "status = 'Paid'" }
current_user.cancled_orders 的方式取到「取消的訂單」了,相當直觀。t = Post.arel_table
results = Post.where(
t[:author].eq("Someone").
or(t[:title].matches("%something%"))
)
rails plugin new <plugin_name>,會產生 plugin 資料夾,結構為:my_plugin
├── Gemfile
├── Gemfile.lock
├── MIT-LICENSE
├── README.rdoc
├── Rakefile
├── lib
│ ├── my_plugin
│ │ └── version.rb
│ ├── my_plugin.rb
│ └── tasks
│ └── my_plugin_tasks.rake
├── my_plugin.gemspec
└── test
│
...(本篇不提到,忽略)
module ZurbFoundation class Engine < Rails::Engine end end你可以將他獨立成一個檔案由
my_plugin.rb 載入,或是直接寫到 my_plugin.rb 裡。有了這段程式碼,Rails就會自動將 app 及 config 兩個資料夾下的所有檔案載入。請注意,載入 engine 的 code 必須放在 my_plugin.rb 的最後,才能正常運作
# CURRENT FILE :: app/controllers/my_plugin/my_controller.rb module MyPlugin class MyController < ::ApplicationController def index ... end end end
# CURRENT FILE :: config/routes.rb Rails.application.routes.draw do get "team" => "team_page/team#index" , :as => :team_page end
gem build /project_path/user_switch.gemspec打包後的 gem 可以 push 到 rubygems.org:
gem push project_name-0.0.1.gem
gem "my_plugin", :path=>"/path/to/your/plugin"
gem "my_plugin", :git=>"https://path/to/your/repo"
gem "my_plugin"
YourUser::Fileclass is not loaded. You have to require it (e.g. inuser.rb).The following happens when ruby/rails seesUser::Infoand evaluates it (simplified; onlyUseris defined yet).
- check if
User::Infois defined - it is not (yet)- check if
Infois defined - it is not (yet)uninitialized constant-> do rails magic to find theuser/info.rbfile and require it- return
User::InfoNow lets do it again forUser::File
- check if
User::Fileis defined - it is not (yet)- check if
Fileis defined - it is (because ruby has a built inFileclass)!- produce a warning, because we've been asked for
User::Filebut got::File- return
::File
user.educations 取得使用者學歷後,還需要再進行 Array.detect 來找對應的階段來顯示「國中讀哪裡」、「高中讀哪裡」,這樣的邏輯實作出來相當冗餘也不好看。class Education::HighSchool < Education ... end
class Education < ActiveRecord::Base
...
class << self
def find_sti_class(type_name)
("Education::"+type_name).constantize()
end
def sti_name
name.demodulize
end
end
end
Education::HighSchool 這個Class。Education::HighSchool資料時,Rails 會在type欄位中存入經過 demodulize 的字串「HighSchool」,而不是 Education::HighSchool。Education::HighSchool model了。