How do I re-pass multiple method arguments in Ruby 1.8.5?
I'm using ruby 1.8.5 and I'd like to use a helper method to help filter a user's preferences like this:
def send_email(user, notification_method_name, *args)
# determine if the user wants this email
return if !user.send("wants_#{notification_method_name}?")
# different email methods have different argument lengths
Notification.send("deliver_#{notification_method_name}", user, *args)
end
This works in ruby 1.8.6, however when I try to do this in 1.8.5 and try to send more than one arg I get an error along the lines of:
wrong number of arguments (2 for X)
where X is the number of arguments that particular method r开发者_开发问答equires. I'd rather not rewrite all my Notification methods - can Ruby 1.8.5 handle this?
A nice solution is to switch to named-arguments using hashes:
def send_email(args)
user = args[:user]
notification_method_name = args[:notify_name]
# determine if the user wants this email
return if !user.send("wants_#{notification_method_name}?")
# different email methods have different argument lengths
Notification.send("deliver_#{notification_method_name}", args)
end
send_email(
:user => 'da user',
:notify_name => 'some_notification_method',
:another_arg => 'foo'
)
精彩评论