-
-
Save ahoward/38d2dfb2657b857d9105bdc478c1df55 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Util.bcall(arg_1, arg_2, &callback) | |
| module Util | |
| def bcall(*args, &block) | |
| call(block, :call, *args) | |
| end | |
| def call(object, method, *args, &block) | |
| args = args_for_arity(args, object.method(method).arity) | |
| object.send(method, *args, &block) | |
| end | |
| def args_for_arity(args, arity) | |
| arity = Integer(arity.respond_to?(:arity) ? arity.arity : arity) | |
| arity < 0 ? args.dup : args.slice(0, arity) | |
| end | |
| end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@ahoward may I know what this does?
I initially thought it as a utility that can be used to call procs and lambdas via this common api where the lambda's argument strictness is bye-passed, like:
I thought using it with a lambda will still succeed by only slicing first 2 args. But it failed in the default lambda's manner.
Also weird is this: you are checking if the
arityvariable responds to:arity(line#14). But the arity param variable comes fromobject.method(method).aritywhich will always be an integer value.The only way the
respond_towill return true is if, somehow,object.method(method).arityis actually a method!But I can't see that happening in any case (except maybe by overriding Method#arity to return another method, but why would anybody do that?)
Please clarify.
Also I've now become your fan after hearing your rubyrogues podcast where you talk about the need for devs to do meaningful contribution. Loved it!