1

Quick question: I want to delegate a bunch of methods to an association in my model:

z13u_methods = [
  :isbn_cleaned,
  :oclc_cleaned,
  :contents_cleaned, 
  :summary_cleaned,
  :title_statement,
  :is_serial?
]

delegate *z13u_methods, :to => :z13u, :prefix => true, :allow_nil => true

This works just fine when I'm running Rails 3.2.13 on Ruby 1.9.3. However, when I run Rails 3.2.13 (the same version) on Ruby 1.8.7, I encounter the following error:

syntax error, unexpected tSYMBEG, expecting tAMPER
  delegate *z13u_methods, :to => :z13u, :prefix => true, ...

where the :to is highlighted.

I guess in Ruby 1.8 the splatted array has to be the final parameters (except for a block name). Is there some other way to to splat an array for this situation?

4

1 回答 1

2

如果您仅z13u_methods用于该delegate呼叫,那么您可以这样做:

delegate_args = [
  :isbn_cleaned,
  :oclc_cleaned,
  :contents_cleaned, 
  :summary_cleaned,
  :title_statement,
  :is_serial?,
  { :to => :z13u, :prefix => true, :allow_nil => true }
]
delegate *delegate_args

我认为这是您需要的基本模式。当然还有其他方法可以到达那里:

delegate *(z13u_methods + [{ :to => :z13u, :prefix => true, :allow_nil => true }])

# If you don't mind changing z13u_methods
delegate *z13u_methods.push(:to => :z13u, :prefix => true, :allow_nil => true)

# If you don't want to change z13u_methods
delegate *z13u_methods.dup.push(:to => :z13u, :prefix => true, :allow_nil => true)
# ---------------------^^^

该主题可能有更多变体,这些只是我想到的几个选项。

至于使用 1.8.7,尽快升级,我认为 1.8.7 已经不支持了。

于 2013-06-19T22:31:42.140 回答