{"id":1691,"date":"2008-01-03T04:48:31","date_gmt":"2008-01-02T20:48:31","guid":{"rendered":"http:\/\/ihower.idv.tw\/blog\/archives\/1691"},"modified":"2011-06-30T13:56:42","modified_gmt":"2011-06-30T05:56:42","slug":"writing-efficient-ruby-code","status":"publish","type":"post","link":"https:\/\/ihower.tw\/blog\/1691-writing-efficient-ruby-code","title":{"rendered":"\u5982\u4f55\u5beb\u51fa\u6709\u6548\u7387\u7684 Ruby Code"},"content":{"rendered":"<p>\u53c8\u4f86\u63a8\u85a6 PDF \u6b66\u529f\u79d8\u7b08\uff0c <a href=\"http:\/\/www.informit.com\/store\/product.aspx?isbn=0321540034&amp;rl=1\">Writing Efficient Ruby Code<\/a>\u3002<\/p>\n<p>Ruby \u662f\u500b\u5f88\u6162\u7684\u8a9e\u8a00\uff0c\u4f46\u6709\u4e9b\u4f5c\u6cd5\u61c9\u7528\u5f97\u7576\uff0c\u9084\u662f\u6703\u6709\u4e0d\u932f\u7684\u6539\u5584\u3002\u4e0d\u904e\u8981\u77e5\u9053\u7a0b\u5f0f\u78bc\u7684\u53ef\u8b80\u6027\u8ddf\u57f7\u884c\u6548\u7387\u6709\u6642\u5019\u662f\u885d\u7a81\u7684\uff0c\u9019\u9ede\u9084\u9700\u62ff\u634f\uff0c\u5c24\u5176 software life cycle \u4e00\u958b\u59cb\u53ef\u8b80\u6027\u6bd4\u8f03\u91cd\u8981\u3002\u6709\u53e5\u6700\u4f73\u5316\u7684<a href=\"http:\/\/lukhnos.org\/blog\/zh\/archives\/575\">\u7d93\u5178\u540d\u8a00<\/a>\u4e00\u5b9a\u8981\u5f15\u4e00\u4e0b:<\/p>\n<p style=\"margin: 1em\"> <span style=\"font-size: 150%\">\u672a\u6210\u5e74\u5c31\u9019\u9ebc\u512a\uff0c\u662f\u4e00\u5207\u90aa\u60e1\u7684\u6839\u6e90<\/span><br \/>\n<span style=\"font-size: 120%\">Premature optimization is the root of all evil<\/span><\/p>\n<p>\u9019\u88e1\u7d00\u9304\u4e00\u4e9b\u770b\u5230\u6709\u8da3\u7684\u4e8b\u60c5\uff0cPDF \u88e1\u6709\u66f4\u8a73\u76e1\u7684 example code\u3002<\/p>\n<p><!--more--><\/p>\n<h3>Instance Variables versus Accessors<\/h3>\n<p>@attrubute \u6bd4 self.attrbute \u5feb(method call \u6bd4\u8f03\u8cb4)\uff0c\u5982\u679c\u4f60\u4e0d\u9700\u8981 public method \u6216\u6709 sub-class \u7684\u9700\u6c42\uff0c\u8acb\u8003\u616e\u4e0d\u8981\u7528 attr_accessor \u7b49\u65b9\u5f0f\u4f86\u5efa\u7acb read\/write method\u3002<\/p>\n<h3>Local Variables Are Cheap<\/h3>\n<p>method \u4e2d\u50b3\u9032\u4f86\u7684\u53c3\u6578\u82e5\u5e38\u7528\uff0c\u53ef\u4ee5\u5148\u5b58\u6210 Local Variables \u518d\u4f86\u591a\u6b21\u4f7f\u7528\u3002<\/p>\n<h3>Interpolated Strnges<\/h3>\n<p>\u65b9\u6cd5\u4e00 s = &#8220;:#{a}.#{b}&#8221; \u6bd4\u65b9\u6cd5\u4e8c s = &#8220;:&#8221; &lt;&lt; a.to_s &lt;&lt; &#8220;.&#8221; &lt;&lt; b.to_s \u5feb\u591a\u56c9!! \u4e0d\u9700\u8981\u591a\u7528 &lt;&lt; method call\u3002\u53e6\u5916\u55ae\u5f15\u865f\u8ddf\u96d9\u5f15\u865f\u6c92\u6709\u6548\u7387\u4e0a\u7684\u5dee\u5225\u3002<\/p>\n<h3>In-Place Updates<\/h3>\n<p>\u76f4\u63a5\u4fee\u6539\u6bd4\u8907\u88fd\u4e00\u4efd\u5feb: gsub! \u6bd4 gsub \u5feb\uff0cmerge! \u6bd4 merge \u5feb\u3002\u4f8b\u5982\u9019\u500b\u7bc4\u4f8b s.gsub().gsub!().gsub! \u800c\u4e0d\u662f s.gsub.gsub.gsub<\/p>\n<p>\u4e0d\u904e\u8981\u5c0f\u5fc3\uff0cRuby \u5c0d\u65bc ! \u7248\u672c\u7684\u65b9\u6cd5\u56de\u50b3\u503c\u6709\u6642\u5019\u4e0d\u662f\u81ea\u5df1(\u5f97\u67e5 API \u78ba\u5b9a\u4e00\u4e0b)\uff0c\u56e0\u6b64\u6c92\u8fa6\u6cd5\u7528\u4e32\u63a5\u5beb\u6cd5\u3002<\/p>\n<h3>Set Versus Array<\/h3>\n<p>\u5982\u679c\u4f60\u53ea\u7528\u5230 Array \u7684 uniq, |, %, &#8211; \u7b49\u7fa4\u96c6\u64cd\u4f5c\uff0c\u8acb\u8003\u616e\u6539\u7528 Set \u6703\u6bd4\u8f03\u5feb\uff0c\u50cf\u662f include? \u5c31\u53ef\u4ee5\u5728 O(1) \u505a\u5b8c\u3002\u800c Hash \u53c8\u6bd4 Array \u8cb4\u3002<\/p>\n<h3>Make Decisions at Load Time<\/h3>\n<p>\u6ce8\u610f\u5230\u5728 module \u6216 class \u7684 definition scope \u4e5f\u662f\u53ef\u4ee5\u57f7\u884c\u7a0b\u5f0f\u7684\uff0c\u800c\u4e14\u53ea\u6709\u7b2c\u4e00\u6b21 require source code \u6642\u6703\u57f7\u884c\u7de8\u8b6f\u4e00\u6b21\u3002<\/p>\n<blockquote><p>class Foo<br \/>\nif C=&#8217;somesetting&#8217;<br \/>\ndef A<span class=\"Apple-tab-span\" style=\"white-space: pre\"><\/span><br \/>\n&#8230;version 1<br \/>\nend<br \/>\nelse<br \/>\ndef A<br \/>\n&#8230;version 2<br \/>\nend<br \/>\nend<br \/>\nend<\/p><\/blockquote>\n<h3>Self-Modifying Code<\/h3>\n<p>\u907f\u514d\u5beb\u51fa\u81ea\u5df1\u4fee\u6539\u81ea\u5df1\u5b9a\u7fa9\u7684\u7a0b\u5f0f(\u4e5f\u4e0d\u597d\u8b80code\u5427)\uff0c\u6539\u7528 singleton \u65b9\u5f0f\u4f86\u505a alias_method \u8ddf remove_method\u3002<\/p>\n<h3>Test Most Frequent Case First<\/h3>\n<p>\u7528 case \u6642\u628a\u8f03\u5e38\u767c\u751f\u7684\u653e\u524d\u9762\uff0c\u82e5\u90fd\u5dee\u4e0d\u591a\uff0c\u628a\u8cb4\u7684\u64cd\u4f5c\u7684\u653e\u5f8c\u9762\u3002<\/p>\n<h3>Optimize Access to Global Constants<\/h3>\n<p>\u5728 Constant \u524d\u9762\u52a0\u4e0a namespace operator :: \u6703\u6bd4\u8f03\u5feb\uff0c\u6e1b\u5c11\u67e5\u8a62\u6642\u9593<\/p>\n<h3>Caching Data in Instance Variables<\/h3>\n<p>\u4f8b\u5982\u5728 controller \u88e1\u9762 def captial_letters { (&#8220;A&#8221;..&#8221;Z&#8221;).to_a }  end \u8acb\u6539\u7528 def captial_letters { @captial_letters ||= (&#8220;A&#8221;..&#8221;Z&#8221;).to_a } end<\/p>\n<h3>Caching Data in Class Variables<\/h3>\n<p>\u627f\u4e0a\uff0c\u5982\u679c data \u8f03\u5927\u53c8\u6301\u7e8c\u5b58\u5728\uff0c\u53ef\u4ee5\u6539\u7528 @@capital_letter = (&#8220;A&#8221;..&#8221;Z&#8221;).to_a \u7136\u5f8c\u5b9a def captial_letters { @@captial_letters }  end\u3002\u5728 ActiveRecord \u4e2d\u4e5f\u53ef\u4ee5\u5148\u628a DB \u7684\u8cc7\u6599\u8b80\u51fa\u4f86\u7576\u4f5c CONSTANTS\uff0c\u5c31\u4e0d\u9700\u8981\u6bcf\u6b21\u90fd\u67e5\u8cc7\u6599\u5eab\u4e86:<\/p>\n<blockquote><p>class  State &lt; ActiveRecord::Base<br \/>\nNAMES_ABBR = self.find(:all).map do { |s| [<a href=\"http:\/\/s.name\" class=\"autohyperlink\">s.name<\/a>,s.abbr] }<br \/>\nend<\/p><\/blockquote>\n<h3>Coding Variable Caching Efficiently<\/h3>\n<p>\u611b\u7528 @var ||= begin &#8230;expr&#8230; end \u800c\u4e0d\u662f @var = begin &#8230;expr&#8230; end unless @var\uff0c\u9019\u6a23\u53ea\u9700\u67e5 @var \u4e00\u6b21\u3002\u4f46\u5982\u679c @var\u8981\u80fd\u5403 nil \u6216 false \u5c31\u53ea\u80fd\u591a\u4e00\u500b boolean \u8b8a\u6578\u4f86\u5e6b\u5fd9\u4e86\u3002<\/p>\n<h3>Initializing Variables with nil<\/h3>\n<p>\u82e5\u6c92\u6709\u7d66\u503c\uff0c\u5c31\u4e0d\u9700\u8981\u521d\u59cb\u6210nil<\/p>\n<h3>Using .nil?<\/h3>\n<p>\u5982\u679c\u8981\u6e2c\u8a66\u7684\u503c\u5df2\u77e5\u4e0d\u662f Boolean\uff0c\u90a3\u5c31\u4e0d\u9700\u8981\u7528 .nil \u591a\u4e00\u500b method call\uff0c\u76f4\u63a5 if x \u5c31\u597d\u4e86\u800c\u4e0d\u662f if x.nil?<\/p>\n<h3>nil? or empty? versus blank?<\/h3>\n<p>ActionPack \u6709\u63d0\u4f9b x.blank?\uff0c\u4e0d\u9700\u8981\u7528 x.nil? || x.empty?<\/p>\n<h3>Using return<\/h3>\n<p>\u96d6\u7136 Ruby \u5f88\u8070\u660e\u6703\u81ea\u52d5\u56de\u50b3\u6700\u5f8c\u904b\u7b97\u7684\u503c\uff0c\u4f46\u660e\u78ba\u5beb return \u6703\u6bd4\u8f03\u5feb (Ruby 1.9\u6709\u6539\u5584)<\/p>\n<h3>Using any?<\/h3>\n<p>\u7528 empty? \u4f86\u6e2c\u8a66 string, array or hash \u800c\u4e0d\u8981\u7528 any?\uff0c\u901f\u5ea6\u5dee\u500b\u5169\u500d (\u800c\u4e14 Ruby 1.9 \u4e5f\u62ff\u6389 any?\u9019\u500bmethod\u4e86)<\/p>\n<h3>Block Local Variables<\/h3>\n<p>\u5728 Ruby 1.8 \u88e1\u9762\uff0c\u5728 Block \u88e1\u9762\u5b58\u53d6 Block \u5916\u9762\u7684 local variable \u7adf\u7136\u6bd4\u88e1\u9762\u7684 local variable \u9084\u5feb!! \u4e0d\u904e Ruby 1.9 \u4fee\u6b63\u9019\u500b\u5947\u602a\u7684\u4e8b\u5be6\u5c31\u662f\u4e86\u3002<\/p>\n<h3>Parallel Assignments<\/h3>\n<p>\u5f88\u65b9\u4fbf\u6c92\u932f\uff0c\u4f46 a,b=1,2 \u904b\u7b97\u9084\u4e1f\u51fa\u4f86 Array [1,2] \u662f\u5783\u573e\u3002(Ruby 1.9 \u4fee\u6b63\u4e86\uff0c\u6539\u4e1f true)<\/p>\n<h3>Date Formatting<\/h3>\n<p>\u7528 parse_date \u628a String \u8b8a\u6210 Date \u662f\u500b\u975e\u5e38\u6602\u8cb4\u7684\u64cd\u4f5c\uff0c\u5efa\u8b70\u4f60\u76f4\u63a5\u7528\u5b57\u4e32\u64cd\u4f5c(\u6b63\u898f\u8868\u793a\u6cd5)\u628a String(\u4f8b\u5982\u5f9eDB\u6488\u51fa\u4f86\u7684db_date) \u8b8a\u6210\u4f60\u60f3\u8981\u7684\u683c\u5f0f\uff0c\u6216\u662f\u5f9e\u4e2d\u62c6\u51fa\u4f60\u60f3\u8981\u7684\u5e74\u6708\u65e5\u3002(PDF \u6709 example code)<\/p>\n<h3>Temporary Data Structure Constants<\/h3>\n<p>\u5982\u679c\u7528\u5230\u4e00\u500b\u66ab\u6642\u7684\u8cc7\u6599\u7d50\u69cb(\u5982Array)\u4f46\u63a5\u4e0b\u4f86\u4e0d\u6703\u66f4\u6539\uff0c\u53ef\u4ee5\u6539\u7528 Constant \u5ba3\u544a\uff0c\u4e26\u907f\u514d\u8907\u88fd\u3002<\/p>\n<h3>ObjectSpace.each_object<\/h3>\n<p>DO NOT USE IT\uff0c\u4f60\u4e0d\u6703\u60f3\u5728 per-request \u4e0b\u53bb\u57f7\u884c\u7684\uff0c\u5f88\u8cb4\u3002<\/p>\n<h3>Unnecessary Block Parameters<\/h3>\n<p>def foo(bar, &amp;block) \u7684 &amp;block \u82e5\u5728\u51fd\u5f0f\u5167\u4e0d\u9700\u8981\u7528\u5230(\u53ef\u4ee5\u76f4\u63a5\u7528 yield \u4f7f\u7528\u7684)\uff0c\u8acb\u4e0d\u8981\u52a0\u3002\u8f49\u6210 Proc \u6bd4\u55ae\u7d14 yield \u7684\u61625\u500d!!<\/p>\n<h3>Symbol.to_proc<\/h3>\n<p>ActiveSupport \u63d0\u4f9b\u7684 @var.map(&amp;:name) \u8a9e\u6cd5\uff0c\u96d6\u7136\u65b9\u4fbf\uff0c\u4f46\u662f\u7528 inline block \u7684\u65b9\u6cd5 @var.map{ |a| <a href=\"http:\/\/a.name\" class=\"autohyperlink\">a.name<\/a>| } \u57f7\u884c\u6548\u7387\u6bd4\u8f03\u5feb\u3002<\/p>\n<h3>Requiring Files Dynamically<\/h3>\n<p>\u5728 Rails \u4e2d\u82e5\u78b0\u5230\u4e00\u500b\u672a\u5b9a\u7fa9\u7684\u5e38\u6578\uff0c\u5b83\u6703\u81ea\u52d5\u53bb load source files \u4f86\u8f09\u5165\u5b9a\u7fa9\u3002\u70ba\u4e86\u907f\u514d\u6df7\u6dc6 file-loading \u6a5f\u5236\uff0c\u4e0d\u9700\u5beb require &#8216;client&#8217;  (\u9019\u53c8\u662f\u53e6\u4e00\u5957 auto-loaded \u6a5f\u5236)\uff0c\u53ea\u8981\u55ae\u55ae\u4e00\u884c\u5beb Client \u5c31\u597d\u4e86\u3002<\/p>\n<h3>Including Modules versus  Opening Classes<\/h3>\n<p>\u4f7f\u7528 module mix-in \u6703\u6bd4\u6253\u958b class \u52a0\u5165 method \u9084\u6162\u4e00\u4e9b\uff0c\u5982\u679c\u4f60\u7684 module \u53ea\u91dd\u5c0d\u4e00\u500b class \u505a mix-in\uff0c\u90a3\u4e0d\u5982\u76f4\u63a5\u6253\u958b\u4f86\u52a0\u5165 methods \u5c31\u597d\u4e86\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u53c8\u4f86\u63a8\u85a6 PDF \u6b66\u529f\u79d8\u7b08\uff0c Writing Efficient Ruby Code\u3002 Ruby \u662f\u500b\u5f88\u6162\u7684\u8a9e &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/ihower.tw\/blog\/1691-writing-efficient-ruby-code\" class=\"more-link\">\u95b1\u8b80\u5168\u6587<span class=\"screen-reader-text\">\u3008\u5982\u4f55\u5beb\u51fa\u6709\u6548\u7387\u7684 Ruby Code\u3009<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,31],"tags":[],"class_list":["post-1691","post","type-post","status-publish","format-standard","hentry","category-programming","category-ruby","entry"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_shortlink":"https:\/\/wp.me\/p1q6tG-rh","jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1691","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/comments?post=1691"}],"version-history":[{"count":3,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1691\/revisions"}],"predecessor-version":[{"id":5953,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/posts\/1691\/revisions\/5953"}],"wp:attachment":[{"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/media?parent=1691"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/categories?post=1691"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ihower.tw\/blog\/wp-json\/wp\/v2\/tags?post=1691"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}