-1

Suppose I have an enum like this:

enum colors: [:black, :dark_gray, :light_gray, :white]

I want the output as:

[["black",0], ["dark_gray",1], ["light_gray",2], ["white",3]]

or as

[["Black",0], ["Dark gray",1], ["Light gray",2], ["White",3]] #using k.humanize on all keys

How can I get this as output. Also, is this possible from a 1-d array as well. eg.

input=[1,2,3]
output=[[1,1],[2,4],[3,9]]

using something like map etc.

I tried using map function, but the way I tried gives invalid syntax. So I asked it here. The answer should be kind of trivial, however I am missing something.

I tried:

op = colors.keys.map{[|s| s, colors[s]]}

but this is wrong syntax.

SyntaxError: unexpected ']', expecting '}'
5
  • What did you try? Commented Apr 12, 2017 at 10:55
  • Your questions is unclear. Ruby doesn't have 2D arrays, so what do you mean by "2D array"? How is your question starting with "Also …" related to your problem? And what do you mean by "using something like map etc"? Why do you want to use "something like map" instead of simply using map? Commented Apr 12, 2017 at 10:56
  • I was trying something like colors.keys.map{[|s| s, colors[s]]} but this is invalid syntax. Commented Apr 12, 2017 at 10:57
  • What have you tried? What is the code you are having trouble with? What is the problem with your code? Do you get an error? If yes, what is the precise error you are getting? Does the actual result differ from the expected result? If yes, what is the result you are expecting, why are you expecting that result, what is the actual result and how do they differ? Does the observed behavior of the code differ from the intended behavior? If yes, what is the intended behavior and why, what is the behavior you are seeing, and how do they differ? What is the specification for the intended behavior? Commented Apr 12, 2017 at 10:57
  • Is it still really unclear what I am asking? Steve's answer is exactly what I need here. Commented Apr 12, 2017 at 11:02

1 Answer 1

5

This facility already exists but you should change your enum to singular

enum color: [:black, :dark_gray, :light_gray, :white]

There is a class method in your model now called colors

MyModel.colors

which will return a hash

You can convert it into an array of arrays with the #to_a method

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

5 Comments

One more thing Steve. What if I want to do some changes to keys here, like humanize eg. So that o/p is [["Black",0], ["Dark gray",1], ["Light gray",2], ["White",3]]
You can just do that with a map.. MyModel.colors.to_a.map{|c|[ c[0].humanize, c[1]]} I'm a bit curious as to why you want this... it's not for a form, is it? A form should return the colors text, not the colors index.
It is for a for a form only. I was thinking that the form would be showing the color names and return the index (corresponding enum value) which is stored in DB. Is this a wrong approach?
enum fields are stored as index, but the value has to be set to the text when updating the record in the database. ActiveRecord does the translation from text to integer and vice versa. You set @model.color = 'black' not @model.color = 0
Ok. Thanks for the help. I understand what you are saying and make the necessary changes.

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.