Is there something like 'with_indifferent_access' for arrays that you can use for include?

I am trying to use only characters: for keywords in my application. I try to strictly choose between: symbol => logic or string => UI / language specific

But I get some "values" (for example, parameters, etc.) for JSON, since there are no characters in JSON: all my generated hashes have the attribute "with_indifferent_access".

but: is there anything equal for an array? like this

a=['std','elliptic', :cubic].with_indifferent_access

a.include? :std => true

?

edit: added rails to tags

+4
source share
2 answers
a = ['std','elliptic', :cubic].map(&:to_sym)
a.include? :std
#=> true

Edit - regarding a maxigs comment, it's probably better to convert to strings:

a = ['std', 'elliptic', :cubic].map(&:to_s)
a.include? "std"
#=> true
+2
source

TL; dg

['std','elliptic', :cubic].flat_map { |s| [s.to_sym, s.to_s] }

Rails . . :

validates :source, inclusion: { in: [:source1, :source2] }

, , , source db . , , . :

validates :source, inclusion: { in: [:source1, :source2].flat_map { |s| [s, s.to_s] } }
0

Source: https://habr.com/ru/post/1527219/


All Articles