Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to simplify split, iterate.each and join in ruby? -- Ruby beginner

words = self.tag.split
words.each { |word| word = word.stem }
self.tag = words.join(' ')

For a given sentence I want to perform the stem action on each individual word.

Is there a way to simplify this code?

like image 605
Jacob Avatar asked Nov 13 '11 14:11

Jacob


2 Answers

self.tag = self.tag.split.map(&:stem).join(' ')
like image 59
J-_-L Avatar answered Oct 26 '22 17:10

J-_-L


self.tag = self.tag.split.collect { |w| word.stem }.join(' ')

Not that I necessarily recommend doing so.

like image 25
Dave Newton Avatar answered Oct 26 '22 18:10

Dave Newton