method-missing

Use Ruby to parse a Tcl DSL

ε祈祈猫儿з 提交于 2019-12-11 10:29:40
问题 I would like to be able to parse some Tcl code where arguments are not surrounded by strings. Consider this tcl code: proc foo {name} { puts "Foo --> $name" } foo bar For those unfamiliar with Tcl, foo is the method name and bar is the argument (quotes are optional in Tcl). The previous code will output: Foo --> bar Is it possible to parse exactly the same input using ruby ( bar remains unquoted)? The equivalent ruby code is: def foo(name) puts "Foo --> #{name}" end tcl = <<-TCL.gsub(/^\s+/,

Is there a “method_missing” for rake tasks?

随声附和 提交于 2019-12-09 15:32:34
问题 If my Rakefile does not find a task with a particular name, I'd like rake to instead create a new task by that name according to certain rules, if a file with the missing task name exists. But if it doesn't, I want to fall back to the default ("Don't know how to build task 'foo'!"). In short, is there a method_missing for Rake? 回答1: I haven't tried it, but a quick search revealed this. If you define a rule with an empty string, you can catch any task that hasn’t been defined elsewhere. This

Method-missing difficulties in C# 4.0: dynamic vs RealProxy

守給你的承諾、 提交于 2019-12-07 02:56:41
问题 Does anyone know of a way to intercept dynamic method calls (particularly those that are going to raise RuntimeBinderException s) with a RealProxy ? I was hoping to catch the exception and implement 'method missing' on top of that, but it appears to be thrown before the interceptor gets a look-in. My test just looks like: dynamic hello = MethodMissingInterceptor<DynamicObject>.Create(); Assert.AreEqual("World", hello.World()); Where World isn't actually implemented on DynamicObject . The

Method-missing difficulties in C# 4.0: dynamic vs RealProxy

感情迁移 提交于 2019-12-05 06:57:57
Does anyone know of a way to intercept dynamic method calls (particularly those that are going to raise RuntimeBinderException s) with a RealProxy ? I was hoping to catch the exception and implement 'method missing' on top of that, but it appears to be thrown before the interceptor gets a look-in. My test just looks like: dynamic hello = MethodMissingInterceptor<DynamicObject>.Create(); Assert.AreEqual("World", hello.World()); Where World isn't actually implemented on DynamicObject . The interceptor is pretty straightforward - I was hoping to check IMethodReturnMessage.Exception for

Objective-C forwardInvocation:

谁说胖子不能爱 提交于 2019-12-05 01:09:53
问题 I often do something like: CoolViewController *coolViewController = [[CoolViewController alloc] init]; [self.navigationController pushViewController:coolViewController animated:YES]; [coolViewController release]; How would I, in a category of UINavigationController , override forwardInvocation: so that I could just instead do: [self.navigationController pushCoolViewControllerAnimated:YES]; Please include the relevant code in your answer, not just an explanation. Thank you! Feel free to

Objective C and magic methods in class

こ雲淡風輕ζ 提交于 2019-12-04 11:59:38
问题 Does objective-c offer a way to intercept calls to class method that does not exist? 回答1: The forwardInvocation method is what you are going to want to use. It is called automatically when a non-existent selector is called on an object. The default behavior of this method is to call doesNotRecognizeSelector: (which is what outputs debug information to your console), but you can override it do anything you want. One recommended approach by Apple is to have this method forward the method

Is there a method_missing in scala?

大城市里の小女人 提交于 2019-12-04 07:02:40
similar to the one in Ruby The following is no longer strictly true with the Dynamic trait found in [experimental] Scala 2.9. See the answer from Kipton Barros, for example. However, Dynamic is still not quite like method_missing , but rather employs compiler magic to effectively rewrite method calls to "missing" methods, as determined statically, to a proxy ( applyDynamic ). It is the approach of statically-determining the "missing" methods that differentiates it from method_missing from a polymorphism viewpoint: one would need to try and dynamically forward (e.g. with reflection) methods to

Is there a “method_missing” for rake tasks?

て烟熏妆下的殇ゞ 提交于 2019-12-04 01:28:32
If my Rakefile does not find a task with a particular name, I'd like rake to instead create a new task by that name according to certain rules, if a file with the missing task name exists. But if it doesn't, I want to fall back to the default ("Don't know how to build task 'foo'!"). In short, is there a method_missing for Rake? I haven't tried it, but a quick search revealed this . If you define a rule with an empty string, you can catch any task that hasn’t been defined elsewhere. This makes it easy to dynamically create rake tasks. Essentially, this is method_missing for rake! rule "" do |t|

Objective-C forwardInvocation:

六眼飞鱼酱① 提交于 2019-12-03 17:23:10
I often do something like: CoolViewController *coolViewController = [[CoolViewController alloc] init]; [self.navigationController pushViewController:coolViewController animated:YES]; [coolViewController release]; How would I, in a category of UINavigationController , override forwardInvocation: so that I could just instead do: [self.navigationController pushCoolViewControllerAnimated:YES]; Please include the relevant code in your answer, not just an explanation. Thank you! Feel free to comment on whether this is good practice. I'm also asking this for educational purposes, but it seems to me

Ruby’s “method_missing” in Python [duplicate]

↘锁芯ラ 提交于 2019-12-03 06:04:49
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Python equivalent of Ruby's 'method_missing' Is there any technique available in Python for intercepting messages (method calls) like the method_missing technique in Ruby? 回答1: As others have mentioned, in Python, when you execute o.f(x) , it's really a two-step operation: First, get the f attribute of o , then call it with parameter x . It's the first step that fails because there is no attribute f , and it's