简体   繁体   中英

Appending a constant Array in the main app from a Rails engine

yes, I want to append a CONSTANT,

I have an array that is stored as a constant in the mainapp, something like

class FOO
  ABILITY = [["xxxx","XX"],["yy","YYY"]]
end

above constant is used throughout the mainapp in various ways.

now, I have a rails engine that extends the mainapps abilities, and would like to extend the ABILITY array in the main app so if I where to do a

ABILITY.each

i would get an extra pair.

Without thinking, I just did a

class BAR
  FOO::ABILITY << ["zzzz","ZZZZ"]
end

of course this didn't do anything.

I have never considered appending a constant, which in its self seems like a bad thing to do, but considering the purpose of the RailsEngine, for it feels like an adequate idea, thanks in advance.

class FOO
  ABILITY = [["xxx","XX"],["yy","yyyy"]]
end

class BAR
  T= FOO::ABILITY << ["zzz","ZZ"]
end

BAR::T
=> [["xxx", "XX"], ["yy", "yyyy"], ["zzz", "ZZ"]]

you were missing a "," in your ABILITY array. Does that help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM