1

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?

2 Answers 2

6

Just define them as constant:

class MyController < AplicationController

  SETUP_STAGES = [:data_entry_completed, :data_validation_completed]
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.