1

I have a rails app that does a lot of JSON parsing (ie using strings as keys rather than symbols).

I have the following code:

ad_source_ids = []
logged_one['migrated'].each { |mig| ad_source_ids << mig['id'] }

I'd like to do

ad_source_ids = logged_one['migrated'].map(&:id)

but don't think I can. What is an alternative? I'd like to removed the ad_source_ids tmp variable.

timpone
  • 19,235
  • 36
  • 121
  • 211
  • 3
    one way is `ad_source_ids = logged_one['migrated'].map {|mig| mig['id'] }` – fanta Apr 16 '19 at 19:31
  • 1
    BTW, you can use `JSON.parse(some_json_str, symbolize_names: true)` to get symbols instead of strings as keys. source: https://apidock.com/ruby/JSON/parse – MrYoshiji Apr 16 '19 at 19:32

1 Answers1

1

You're almost there. Try this:

ad_source_ids = logged_one['migrated'].collect { |mig| mig['id'] }
chanko
  • 269
  • 1
  • 5