chell chell - 1 month ago 6
Ruby Question

How to get the second element of an array in an array ruby

I have a set as follows:

verb_tag_set = Set.new ["VB", "VBD", "VBG", "VBN", "VBP", "VBZ", "MD"]


My tagged_text array contains the following:

tagged_text = [["VB", "go"], ["VBG", "going"]]


I am trying to get all of the elements of the second row of each array by selecting those arrays that contain an element that matches one of the items in the verb_tag_set.

verb_tagged_array = tagged_text.select{|el| el[1] if verb_tag_set.include?(el[0])}
verb_tagged_array.map{|row| row[1]}


Although this works, I should be able to get the array in one line.

Any ideas on how to refine this code?

NOOB with ruby so any help appreciated.

Answer

It appears you want the following.

require 'set'

verb_tag_set = Set.new ["VB", "VBD", "VBG", "VBN", "VBP", "VBZ", "MD"]    
tagged_text = [["VB", "go"], ["VBG", "going"]]

tagged_text.select { |a,b| verb_tag_set.include?(a) }.map(&:last)
  #=> ["go", "going"]