array iterate grammar rules

  • Thread starter Michael Linfield
  • Start date
M

Michael Linfield

If i had an array containing a lot of words, and i wanted to add grammar
rules to those words, ie make words plural how would i duplicate the
original element and then modify that element with the grammar rule, and
id hope to iterate this for all the words and apply grammar rules via a
defined method.

IE:

array = ["sky","knife","kitty"]

after applying gsub or some other method such as inject or collect...the
array would become...

array #=> ["sky","skies","knife","knives","kitty","kitties"]
and whatnot ect ect.

id rather not gsub all the grammar rules lol.

Any ideas?

Thanks!

-Mac
 
P

Phrogz

If i had an array containing a lot of words, and i wanted to add grammar
rules to those words, ie make words plural how would i duplicate the
original element and then modify that element with the grammar rule, and
id hope to iterate this for all the words and apply grammar rules via a
defined method.

IE:

array = ["sky","knife","kitty"]

after applying gsub or some other method such as inject or collect...the
array would become...

array #=> ["sky","skies","knife","knives","kitty","kitties"]
and whatnot ect ect.

id rather not gsub all the grammar rules lol.
irb(main):001:0> require 'rubygems'
=> true
irb(main):002:0> require 'active_support'
=> true
irb(main):003:0> array = ["sky","knife","kitty"]
=> ["sky", "knife", "kitty"]
irb(main):004:0> plurals = array.map{ |word| word.pluralize }
=> ["skies", "knives", "kitties"]
irb(main):005:0> array
=> ["sky", "knife", "kitty"]
irb(main):006:0> array.zip( plurals )
=> [["sky", "skies"], ["knife", "knives"], ["kitty", "kitties"]]
irb(main):007:0> array.zip( plurals ).flatten
=> ["sky", "skies", "knife", "knives", "kitty", "kitties"]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,416
Messages
2,571,562
Members
48,797
Latest member
shadowoftheunknown

Latest Threads

Top