TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

Ruby Tricks

47 pointsby vijaydevover 15 years ago

5 comments

petercooperover 15 years ago
Just want to note, as the author, that this post is almost 18 months old now and I don't necessarily stand by them all anymore ;-) (I'd need to go back and re-edit to make <i>that</i> assertion..)
评论 #811939 未加载
评论 #811980 未加载
gommmover 15 years ago
Careful with one of those tips [* x] does return x if x is an array and [x] otherwise except in the case where x is a hash in which case it transforms the hash to an array. Eg:<p>x = {:a =&#62; 1, :b =&#62; 2}<p>[* x]<p>=&#62; [[:a, 1], [:b, 2]]<p>Some of the other tips just make the code less readable
评论 #811731 未加载
chasingsparksover 15 years ago
I often do the %w{rubygems sinatra etc}.each{|lib| require lib} trick. If I were more motivated, I would submit a patch that would allow require to take an array.
评论 #812350 未加载
blasdelover 15 years ago
A trick is something a whore does for money, not something that improves your code.
axodover 15 years ago
<p><pre><code> &#62;&#62; 3. Format decimals &#62;&#62; money = 9.5 &#62;&#62; "%.2f" % money # =&#62; "9.50" </code></pre> Seriously? IMHO These sorts of things are not nice. To read the code you have to know these special magic operators. It's not immediately obvious it's calling a function to do string formatting.<p>Readable code is so important :/
评论 #811926 未加载