TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

Ruby Tips, Part 5

80 点作者 timblair大约 11 年前

5 条评论

bstar77大约 11 年前
My favorite Ruby resources: <a href="http://www.confreaks.com" rel="nofollow">http:&#x2F;&#x2F;www.confreaks.com</a> | <a href="http://www.rubytapas.com" rel="nofollow">http:&#x2F;&#x2F;www.rubytapas.com</a> | <a href="https://www.destroyallsoftware.com/screencasts" rel="nofollow">https:&#x2F;&#x2F;www.destroyallsoftware.com&#x2F;screencasts</a> | <a href="http://railscasts.com" rel="nofollow">http:&#x2F;&#x2F;railscasts.com</a> (dated but still useful) | <a href="https://www.ruby-toolbox.com" rel="nofollow">https:&#x2F;&#x2F;www.ruby-toolbox.com</a>
ultimoo大约 11 年前
I think growing a string with &lt;&lt; is an expensive operation. Since once the allocated memory is full, it will cost O(n) to copy the entire string to a new location. If we are carrying out m insertions, this could take O(mn).<p>The way I have been building strings is to store the m strings in an Array and then join() it at the end (which should be only O(n)). The m small objects need to be allocated in both cases, the only difference being that we hold on to those and join them later on.<p>Am I missing something? I have never measured the performance on either of the these operations so I could be off the mark here.
评论 #7630353 未加载
danso大约 11 年前
Something in my computer-science-student background feels unsettled when I can use a language for years and still learn new tricks (or often, that I&#x27;ve forgotten that I&#x27;ve learned) about common functionality such as string interpolation...but I do love a lot of Ruby&#x27;s tricks.<p><pre><code> if path =~ %r{^&#x2F;assets&#x2F;mobile&#x2F;img} ... end </code></pre> I knew about `%r` but hadn&#x27;t realized that it escapes forward-slashes while leaving other regex symbols intact. But with the previous explanation of `%q`, in which arbitrary delimiters can be used, it makes sense...curly braces simply replace `&#x2F;` as the delimiter, and `&#x2F;` simply retains its non-specialness in regex...saves me a lot of writing `Regex.escape(&quot;&#x2F;path&#x2F;to&quot;)`.<p>Another regex notation that I recently learned (elsewhere) and now love to abuse:<p><pre><code> &quot;He is 32-years old&quot;[&#x2F;\d+&#x2F;] # =&gt; &quot;32&quot; </code></pre> as opposed to:<p><pre><code> &quot;He is 32-years old&quot;.match(&#x2F;\d+&#x2F;)[0]</code></pre>
评论 #7627112 未加载
评论 #7627491 未加载
评论 #7627924 未加载
评论 #7627341 未加载
rickdale大约 11 年前
These are some good tips. If you are into the Ruby language and want more, I recommend checking out Ruby Tapas <a href="http://www.rubytapas.com/" rel="nofollow">http:&#x2F;&#x2F;www.rubytapas.com&#x2F;</a> . I have no affiliation with it, just happy customer.
riffraff大约 11 年前
notice that this is wrong<p><pre><code> x ||= y # equivalent to: x || x = y </code></pre> it&#x27;s actually something slightly different (or at least it was some years ago).
评论 #7630404 未加载