How to find position in an array

I have an array:

r1.rp_parts:
[<Rp1…>][<Rp1…>][<Rp1…>][<Rp4…>]

If I do:
r1.rp_parts[2]
I obtain
Rp1…

I would like to obtain de position of r1.rp_parts[2] in the r1.rp_parts
array. I know that it’s position is 2, but I wonder if I can obtain the
position from r1.rp_parts[2].

John S. wrote:

I have an array:

r1.rp_parts:
[<Rp1…>][<Rp1…>][<Rp1…>][<Rp4…>]

If I do:
r1.rp_parts[2]
I obtain
Rp1…

I would like to obtain de position of r1.rp_parts[2] in the r1.rp_parts
array. I know that it’s position is 2, but I wonder if I can obtain the
position from r1.rp_parts[2].

Use the index method of Array:

some_part = r1.rp_parts[2]
r1.rp_parts.index(some_part) #=> 2

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs