How to Reference Objects in an Array

Asked 2 years ago, Updated 2 years ago, 37 views

temp=[
  {
    —hoge=>1,
    —huga=>[ ]
  },
  {
    —hoge=>2,
    —huga=>[ ]
  },
]

Is there a "hoge refers to an object with 1" in this array?

As an image, I would like to do the following:

temp[{:hoge==2}][:huga]<<123

[
  {
    —hoge=>1,
    —huga=>[ ]
  },
  {
    —hoge=>2,
    —huga=>[123]
  },
]

ruby

2022-09-30 17:01

1 Answers

You can use Enumerable#find to:

temp.find {|h|h[:hoge]==1}[:huga]<<123


2022-09-30 17:01

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.