chell chell - 1 year ago 84
Ruby Question

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

I have a set as follows:

verb_tag_set = ["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 ={|el| el[1] if verb_tag_set.include?(el[0])}{|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 Source

It appears you want the following.

require 'set'

verb_tag_set = ["VB", "VBD", "VBG", "VBN", "VBP", "VBZ", "MD"]    
tagged_text = [["VB", "go"], ["VBG", "going"]] { |a,b| verb_tag_set.include?(a) }.map(&:last)
  #=> ["go", "going"] 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download