-
Notifications
You must be signed in to change notification settings - Fork 147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Idea: expand the PM::Support.try method #735
Comments
Haven't tested but it looks like we might be able to do something like this to support as many arguments as we want. def try(method, *args)
if respond_to?(method)
arity = self.method(method).arity
if arity == 0
self.send(method)
else
self.send(method, *args.first(arity))
end
end
end |
The above works flawlessly in my tests :P |
I like it! Have you done any benchmark tests? |
I haven't, thought I just realized that we also have the |
I really like it. Keep in mind that any optional arguments will screw this up badly. Really wish Ruby had |
I'm doing something kind of similar to your arity trick here: https://github.com/clearsightstudio/ProMotion/blob/master/lib/ProMotion/repl/live_reloader.rb#L18 |
We use
try
a lot internally, but we're also starting to do a lot ofarity
detection to determine what parameters should be sent to the user's subclass like so:what if we expanded the
try
method to do something like this?so that we could call something like
try(:some_method, var1, var2, var3)
and if the user has only implementeddef some_method(); end;
it wouldn't send those other arguments, but if they implementeddef some_method(var1); end;
ordef some_method(var1, var2); end;
it would still work.Thoughts?
The text was updated successfully, but these errors were encountered: