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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Do you know Ruby’s ‘Chainsaw’ method?

46 点作者 IndianGuru超过 14 年前

11 条评论

JangoSteve超过 14 年前
I prefer to use define_method over method_missing when "metaprogramming". It allows you to explicitly define your "metamethods" up-front.<p>Using his example of the DoNotDisturb wrapper around InformationDesk, you could easily do something like:<p><pre><code> [:emergency, :flights, :local_transports, :hotels].each do |name| define_method(name) do check_lunch_break unless name == :emergency @desk.send(name) end end </code></pre> Now you don't get the side effect of screwing up your exception handling and having to walk on eggshells with the rest of your coding in that class.<p>The other thing I like about this method is that it gets evaluated once, and then those methods exist. With method_missing, every single time you call one of those methods, it has to go all the way up the chain to find that method until it finally hits method_missing.<p>EDIT: Oh, and if nothing else, you can at least put the define_method inside the method_missing (I had a project that required method_missing once, so this is what I did). That way, it only has to crawl all the way up the inheritance chain once, then that method gets defined on your class, so subsequent calls to that method don't have to go all the way up the chain again.<p>Might not be much performance gain in Ruby, but when programming a rails app (where you're likely working on a class that inherits from ActiveRecord and 10 other classes), it helps.
评论 #1766710 未加载
评论 #1767131 未加载
评论 #1766788 未加载
评论 #1766779 未加载
评论 #1767542 未加载
jhickner超过 14 年前
There's actually a fairly safe way to use method_missing. Just remember to call super if none of your conditions are met, that way you'll still get errors when commands are misspelled. You really shouldn't ever use method_missing without that.<p>He probably also should be checking if @desk actually has a method with the provided name (with @desk.respond_to?(method_name)) before attempting to call it.
评论 #1767004 未加载
评论 #1766667 未加载
jrockway超过 14 年前
method_missing / AUTOLOAD is a great way to break any tools that rely on being able to introspect your classes.<p>For something as simple as this, just add the methods to the class in a loop. Then you class works just like every other class; no introspection is broken. It's also faster at runtime.
评论 #1766957 未加载
jfager超过 14 年前
<i>When I work with Java or C#, I accept this kind of code duplication as a fact of life.</i><p>Java has had dynamic proxies since 1.4. As long as you're working with interfaces, you can do the exact same thing as the example given in this post.
评论 #1766787 未加载
xentronium超过 14 年前
For me method_missing looks more like an anti-pattern because it's easy to miss, it's hard to debug, it's impossible to scan visually, it messes up code lookup if you use IDE, it messes up search if you don't.
bluesnowmonkey超过 14 年前
A better analogy to magic methods would be dynamite. There are a few situations where you should use it, but not many, and generally it's when there's no other option.<p>His example violates the principle that objects should represent things. (What is a DoNotDisturb?) Back to OOP school, metaprogrammer!
评论 #1767418 未加载
callmeed超过 14 年前
I'm going through the author's book right now (<i>Metaprogramming Ruby</i>). It's great.
zeteo超过 14 年前
FP fanboys will laugh so hard at this. The proper way to deal with duplication, as described in this article, is of course to use functors. With a general method_missing(), your compiler will not even catch spelling errors anymore.
评论 #1766673 未加载
joshd超过 14 年前
"I use method_missing()to remove duplication ... On the other hand, I usually think twice about using method_missing() for cosmetic reasons, like getting cool method names such as find_by_name_and_address()."<p>That seems backwards to me. I use magic methods to define methods that can only be defined at run time, like his example of creating methods based on schema introspection.<p>Using magic methods breaks a lot of tools: IDE code completion, reflection, documentation generation so I always explicitly define methods unless it's not possible.
pkulak超过 14 年前
I've found that the only good time to use method_missing is when I'm wrapping an object and need to send all but a couple methods through to it untouched. I've really never used it for anything else.
scott_s超过 14 年前
I don't do any Ruby, but: can someone give a non-contrived situation where you would want to do this? What problem does this solve?
评论 #1766869 未加载
评论 #1767308 未加载