PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.ruby > array iterate grammar rules
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
array iterate grammar rules

Réponse
 
LinkBack Outils de la discussion
Vieux 07/11/2007, 04h16   #1
Michael Linfield
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut array iterate grammar rules

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","kittie s"]
and whatnot ect ect.

id rather not gsub all the grammar rules lol.

Any ideas?

Thanks!

-Mac
--
Posted via http://www.ruby-forum.com/.

  Réponse avec citation
Vieux 07/11/2007, 04h58   #2
Phrogz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: array iterate grammar rules

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","kittie s"]
> 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"]


  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h15.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,09173 seconds with 10 queries