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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Hacking Ruby's Default Arguments

2 点作者 pwim大约 16 年前

1 comment

jamesbritt大约 16 年前
I don't quite see this as "hacking" anything, but it's a useful thing to know.<p>Along similar lines, I was coding some Ruby and calling a method that took a flag argument. E.g.<p><pre><code> save_something @something, true </code></pre> and it bothered my that it was not obvious what that last value was for.<p>I figured that one approach might be to create a throwaway variable and use it in place of the literal:<p><pre><code> overwrite_existing = true save_something @something, overwrite_existing </code></pre> ... and then decided to just pass that first expression in as the argument itself to tighten things up:<p><pre><code> save_something @something, overwrite_existing = true </code></pre> Basically, a handy (hackish?) way to document any argument.