如何在Ruby 1.8.5中重新传递多个方法参数?

如何在Ruby 1.8.5中重新传递多个方法参数?,ruby,ruby-1.8,Ruby,Ruby 1.8,我使用的是ruby 1.8.5,我想使用一个helper方法来帮助过滤用户的首选项,如下所示: 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

我使用的是ruby 1.8.5,我想使用一个helper方法来帮助过滤用户的首选项,如下所示:

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
这在ruby 1.8.6中起作用,但是当我在1.8.5中尝试这样做并尝试发送多个arg时,会出现如下错误:

X的参数2的数目错误


其中X是特定方法所需的参数数。我不想重写我所有的通知方法-Ruby 1.8.5能处理这个问题吗?

一个很好的解决方案是使用哈希值切换到命名参数:

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'
)

出于好奇,为什么不使用Ruby 1.8.6呢?为什么不使用Ruby 1.8.7呢?这是Ruby 1.8的最新版本。Ruby的旧版本有标签吗?升级会很好。不幸的是,这不符合我当前项目的范围。或者回答我在1.8.5中关于如何执行此操作的问题: