0

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 requires. I'd rather not rewrite all my Notification methods - can Ruby 1.8.5 handle this?

Phrogz
  • 296,393
  • 112
  • 651
  • 745
ddux
  • 1

1 Answers1

0

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'
)
the Tin Man
  • 158,662
  • 42
  • 215
  • 303