문제

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?

도움이 되었습니까?

해결책

If you're only using z13u_methods for that delegate call then you could do this:

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

I think that's the basic pattern that you need. There are other ways to get there of course:

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)
# ---------------------^^^

There are probably more variations on that theme, those are just a couple options that come to mind.

As far as using 1.8.7 is concerned, upgrade ASAP, I don't think 1.8.7 is even supported anymore.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top