Reputation: 1182
I have a set of boolean symbols, inside a controller action
These are currently in 1 action, in the following format
def my_action
setup_stages = [:data_entry_completed, :data_validation_completed]
setup_stages.each do |stage|
do stuff
end
end
I've noticed that I need to make use of these symbols in another action but do not want to replicate them. Is there a way to make this list accessible to multiple actions in the controller so that I can iterate through them without having the list twice?
Upvotes: 1
Views: 174
Reputation: 332
I would personally define it as an instance variable:
class MyClass
def initialize
@setup_stages = [:data_entry_completed, :data_validation_completed]
end
def do_action
@setup_stages.each do |stage|
# do stuff
end
end
def show_stages
puts @setup_stages.to_s
end
end
x = MyClass.new
x.do_action
x.show_stages
A constant is also a good way of defining this but should not be altered, so if for whatever reason you want to add other options to the array dynamically you would be able to do this with an instance variable.
Upvotes: 0