i have the following ruby class (user.rd) that has an enum (UserStatus):
class User< ActiveRecord::Base
end
class UserStatus
NEW = "new"
OLD = "old"
DELETED = "deleted"
end
is there a way i can iterate over all the enum values?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What you have created there are called ‘constants’, not enumerations. As Zabba said, “Ruby does not have ‘enum’.” If you must keep this data structure, if you are already using the constants in your code, then you can iterate them like so:
The use of
falseabove is needed to prevent you from getting constants defined in superclasses:If you are not married to the use of individual constants, you might be interested in creating a frozen Hash of immutable values instead: