lookups in array

4 views
Skip to first unread message

fugee ohu

unread,
May 15, 2018, 1:10:14 PM5/15/18
to Ruby on Rails: Talk
I have an array of US States that looks like    STATE_TYPES =   [    ['Alabama', 'AL'], ... ]
In views I need to display the State name from the value I have in the database, it's abbreviation

Walter Lee Davis

unread,
May 15, 2018, 2:40:19 PM5/15/18
to rubyonra...@googlegroups.com
If you can put those into a hash, then you can look up values by their keys, or vice-versa. Just call STATE_TYPES.to_h and then you have a hash where the names are the keys, and the abbreviations are the values. If you call STATE_TYPES.map(&:reverse).to_h, you will have the abbreviations as keys and the names as values. In either case, you could then use index notation to get a given key's value:

STATE_TYPES.to_h['Arizona']
=> 'AZ'

STATE_TYPES.map(&:reverse).to_h['AZ']
=> 'Arizona'

You don't need to call map or to_h every time, just make a new constant and use it over and over:

STATES = STATE_TYPE.map(&:reverse).to_h

STATES['AZ']
=> 'Arizona'

Walter

> On May 15, 2018, at 1:10 PM, fugee ohu <fuge...@gmail.com> wrote:
>
> I have an array of US States that looks like STATE_TYPES = [ ['Alabama', 'AL'], ... ]
> In views I need to display the State name from the value I have in the database, it's abbreviation
>
> --
> You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
> To post to this group, send email to rubyonra...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d7eea706-b4fa-4413-82e3-fda20742fb32%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Rob Biedenharn

unread,
May 15, 2018, 2:57:30 PM5/15/18
to rubyonra...@googlegroups.com

On 2018-May-15, at 14:39 , Walter Lee Davis <wa...@wdstudio.com> wrote:

If you can put those into a hash, then you can look up values by their keys, or vice-versa. Just call STATE_TYPES.to_h and then you have a hash where the names are the keys, and the abbreviations are the values. If you call STATE_TYPES.map(&:reverse).to_h, you will have the abbreviations as keys and the names as values. In either case, you could then use index notation to get a given key's value:

STATE_TYPES.to_h['Arizona']
=> 'AZ'

STATE_TYPES.map(&:reverse).to_h['AZ']
=> 'Arizona'

You don't need to call map or to_h every time, just make a new constant and use it over and over:

STATES = STATE_TYPE.map(&:reverse).to_h

STATES['AZ']
=> 'Arizona'

Walter

Or you can take advantage of some other Ruby behavior:

The Hash[] constructor which will build a Hash from an Array of pairs (i.e., Arrays of two elements)
Hash#invert which returns a new hash with the values and keys swapped.

[3] pry(main)> STATE_TYPES = [ ['Alabama','AL'], ['Arizona','AZ'], ['Ohio','OH'] ]
=> [["Alabama", "AL"], ["Arizona", "AZ"], ["Ohio", "OH"]]
[4] pry(main)> Hash[STATE_TYPES].invert
=> {"AL"=>"Alabama", "AZ"=>"Arizona", "OH"=>"Ohio"}
[5] pry(main)> Hash[STATE_TYPES].invert['OH']
=> "Ohio"

You might also want to look up Array#assoc and Array#rassoc which will operate directly on your array of pairs:

[6] pry(main)> STATE_TYPES.rassoc('OH')
=> ["Ohio", "OH"]
[7] pry(main)> STATE_TYPES.rassoc('OH').first
=> "Ohio"

-Rob


On May 15, 2018, at 1:10 PM, fugee ohu <fuge...@gmail.com> wrote:

I have an array of US States that looks like    STATE_TYPES =   [    ['Alabama', 'AL'], ... ]
In views I need to display the State name from the value I have in the database, it's abbreviation

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/d7eea706-b4fa-4413-82e3-fda20742fb32%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-ta...@googlegroups.com.
To post to this group, send email to rubyonra...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages