3
class CAR 
   FORD = 1
   GM = 2
   BMW = 3
end

I want to create an array like:

all_cars = [CAR::FORD, CAR::GM, CAR::BMW]
=>[1, 2, 3]

Is there a way I can initialize this array with typing CAR:: for each element, something like

all_cars = %(FORD GM BMW).map {|ele| "CAR::" + ele}
=>["CAR::FORD", "CAR::GM", "CAR::BMW"]

Not want I wanted

4 Answers 4

17

Kind of like Phrogz answer, you could define the constants and initialize the array all at once like this:

class Car
  MAKES = [
    FORD = 1,
    GM = 2,
    BMW = 3
  ].freeze
end

That way, you'd have access to not only individual namespaced constants, but you have an array of them all, as a constant, without the need for repetition:

Car::MAKES # => [1, 2, 3]
Car::FORD  # => 1

Don't forget the freeze or people can mess with your array. You could always dup it if you need to modify it within a particular operation.

Sign up to request clarification or add additional context in comments.

Comments

5
  1. Instead of creating an array of constants outside the class, I usually create such collections inside the class itself. In this case, you have no problem:

    class Car
      FORD = 1
      GM   = 2
      BMW  = 3
      MAKES = [ FORD, GM, BMW ]
    end
    
    p Car::MAKES
    #=> [1, 2, 3]
    
  2. But if you are still set on doing what you propose, you want Module#const_get:

    all = %w[ FORD GM BMW ].map{ |k| Car.const_get(k) }
    #=> [1, 2, 3]
    

Comments

3
%w(FORD GM BMW).map{|x| CAR.class_eval(x)} # => [1, 2, 3]

or

%w(FORD GM BMW).map{|x| eval("CAR::#{x}")} # => [1, 2, 3]

Comments

0

This will do it using a module instead of a class:

module CAR 
   FORD = 1
   GM   = 2
   BMW  = 3
end

include CAR

CAR::FORD # => 1
CAR::GM   # => 2
CAR::BMW  # => 3

all_cars = [CAR::FORD, CAR::GM, CAR::BMW]
all_cars # => [1, 2, 3]

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.