Ruby similar elements from array

Hi all,

How to get similar elements from a array in Ruby.

eg : [[1,2],[11,2],[23,89]]

when i give input 1 i should get all arrays like [[1,2],[11,2]]

Thanks in advance…

lekha p. wrote in post #1167543:

Hi all,

How to get similar elements from a array in Ruby.

eg : [[1,2],[11,2],[23,89]]

when i give input 1 i should get all arrays like [[1,2],[11,2]]

Thanks in advance…

2.1.2 :001 > input = 1
=> 1
2.1.2 :002 > [[1,2],[11,2],[23,89]].select { |a| a.any? { |i| i.to_s =~
/#{input}/ } }
=> [[1, 2], [11, 2]]
2.1.2 :003 >

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