[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

array iterate grammar rules

Michael Linfield

11/7/2007 4:16:00 AM

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
--
Posted via http://www.ruby-....

1 Answer

Phrogz

11/7/2007 4:58:00 AM

0

On Nov 6, 9:16 pm, Michael Linfield <globyy3...@hotmail.com> wrote:
> 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"]