Ruby的25個(gè)編程細(xì)節(jié)大家都知道嗎?其實(shí)細(xì)節(jié)處理好的時(shí)候我們在日常工作中可能會(huì)減少一些麻煩,今天武林技術(shù)頻道小編帶你分享,希望能幫到你。
1.try 永遠(yuǎn)不會(huì)拋出異常 在 沒有的時(shí)候 返回 nil
?
2.find(:first, :condotions) 方法 不言而與
?
3.find(:all, :select, :conditions)
?
4.發(fā)送post請(qǐng)求 可以在shell中執(zhí)行?
?
?
5.Ruby 中純數(shù)據(jù)結(jié)構(gòu) ( Struct 與 OpenStruct )
講一下它倆之間的區(qū)別:
Struct 需要開頭明確聲明字段; 而 OpenStruct 人如其名, 隨時(shí)可以添加屬性
Struct 性能優(yōu)秀; 而 OpenStruct 差點(diǎn), 具體的性能差距可看這里:http://stackoverflow.com/questions/1177594/ruby-struct-vs-openstruct
Struct 是 Ruby 解釋器內(nèi)置, 用 C 實(shí)現(xiàn); OpenStruct 是 Ruby 標(biāo)準(zhǔn)庫, Ruby 實(shí)現(xiàn)
API 不同: Struct API 與 OpenStruct
6. MIme::Type.register
?
?
7.config/initializers/secure_problem_solved.rb
?
8.config/initializers/new_rails_default.rb
?
? # Store the full class name (including module namespace) in STI type column.
? ActiveRecord::Base.store_full_sti_class = true
end
ActionController::Routing.generate_best_match = false
# Use ISO 8601 format for JSON serialized times and dates.
ActiveSupport.use_standard_json_time_format = true
# Don't escape HTML entities in JSON, leave that for the #json_escape helper.
# if you're including raw json in an HTML page.
ActiveSupport.escape_html_entities_in_json = false
?
9.MemCacheStore 緩存
?
localhost::callback_lock
@_cache.write(pay_channel.channel_id,'true')
v = @_cache.read(pay_channel.channel_id)
if v.nil? || v != 'true'
????? return false
??? else
????? return true
??? end
end
?
10.聯(lián)合索引
?
https://github.com/momoplan
?
0.Hash assert_valid_keys 白名單
11.puma -C puma_service_qa.rb
12.pow
13. Time
?
?
14.merchant.instance_of? MplusMerchant
?
?
15.will_paginate rails
安裝之后需要修改config/environment.rb文件
在文件的最后添加:
?
最好有個(gè)include
?
16. # Excel Generator
?
? MONTH = 1.upto(12).to_a
? def self.total_to_xls(year = '2012', opts = {})
??? book = Spreadsheet::Workbook.new
??? sheet1 = book.create_worksheet
??? months = MONTH
??? months = opts[:month].to_s.split(/,/) if opts[:month]
??? fixed_row = months.collect{ |m| m.to_s + '月' }.insert(0, '')
??? sheet1.row(0).concat(fixed_row)
??? row1 = ['']
??? (months.size - 1).times { row1 << ['用戶數(shù)', '金額', '訂單數(shù)'] }
??? sheet1.row(1).concat(row1.flatten!)
??? row = 2
??? sheet1.row(row).insert(0, '全國')
??? months.each_with_index do |m, i|
????? sheet1.row(row).insert(i*3 + 1, self.monthly_users_count(m))
????? sheet1.row(row).insert(i*3 + 2, self.monthly_amount(m))
????? sheet1.row(row).insert(i*3 + 3, self.monthly_orders_count(m))????
??? end
??? PROVINCE.each do |province|
????? row += 1
????? sheet1.row(row).insert(0, province)
????? months.each_with_index do |m, i|
??????? sheet1.row(row).insert(i*3 + 1, self.monthly_users_count_by_province(m, province))
??????? sheet1.row(row).insert(i*3 + 2, self.monthly_amount_by_province(m, province))
??????? sheet1.row(row).insert(i*3 + 3, self.monthly_orders_count_by_province(m, province))
????? end??
??? end
??? path = "tmp/phone_recharge.xls"
??? book.write path
??? path
? end
?
17. inject({})
?
18.time_str.instance_of?
?
?
19.Person.instance_eval
?
?
20.class_eval
?
?
?
21.Ruby中 respond_to? 和 send 的用法
http://galeki.is-programmer.com/posts/183.html
因?yàn)閛bj對(duì)象沒法響應(yīng)talk這個(gè)消息,如果使用 respond_to? 這個(gè)方法,就可以實(shí)現(xiàn)判斷對(duì)象能否響應(yīng)給定的消息了
?
22.method_missing,一個(gè) Ruby 程序員的夢中情人
?
?
?
23.chomp
chomp方法是移除字符串尾部的分離符,例如/n,/r等...而gets默認(rèn)的分離符是/n
24. hash.each_pair{|k,v|} & send()
?
?
?
25.config.middleware 通過 rake -T 可以查看, 在config/ - 去除不必的 middleware
26.1.day.ago.strftime('%Y%m%d')
通過上面的介紹,想必大家對(duì)于Ruby的25個(gè)編程細(xì)節(jié)已經(jīng)有了足夠的了解了,希望大家多多支持武林技術(shù)頻道!
新聞熱點(diǎn)
疑難解答
圖片精選