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 > I don't really understand inject
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
I don't really understand inject

Réponse
 
LinkBack Outils de la discussion
Vieux 19/06/2008, 14h12   #1
Mike Barton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut I don't really understand inject

I think these methods should both do the same thing. The second works,
however in the first I get errors that string is not matched, where
hash is an instance of String not hash

def sequences
self.alignment.split(/\n/).inject(Hash.new) do |hash, line|
id, sequence = line.split(/\s+/,2)
# hash.class == String
hash[id] = sequence
end
end

def sequences
hash = Hash.new
self.alignment.split(/\n/).each do |line|
id, sequence = line.split(/\s+/,2)
# hash.class == Hash
hash[id] = sequence
end
hash
end



  Réponse avec citation
Vieux 19/06/2008, 14h32   #2
Rob Biedenharn
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: I don't really understand inject

On Jun 19, 2008, at 9:13 AM, Mike Barton wrote:

> I think these methods should both do the same thing. The second works,
> however in the first I get errors that string is not matched, where
> hash is an instance of String not hash
>
> def sequences
> self.alignment.split(/\n/).inject(Hash.new) do |hash, line|
> id, sequence = line.split(/\s+/,2)
> # hash.class == String
> hash[id] = sequence

hash # value of block will be the last expression
>
> end
> end
>
> def sequences
> hash = Hash.new
> self.alignment.split(/\n/).each do |line|
> id, sequence = line.split(/\s+/,2)
> # hash.class == Hash
> hash[id] = sequence
> end
> hash
> end


The value of the block becomes the first block parameter in the next
iteration.

-Rob

Rob Biedenharn http://agileconsultingllc.com
Rob@AgileConsultingLLC.com



  Réponse avec citation
Vieux 19/06/2008, 14h36   #3
Jesús Gabriel y Galán
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: I don't really understand inject

On Thu, Jun 19, 2008 at 3:13 PM, Mike Barton <barton.michael@gmail.com> wrote:
> I think these methods should both do the same thing. The second works,
> however in the first I get errors that string is not matched, where
> hash is an instance of String not hash
>
> def sequences
> self.alignment.split(/\n/).inject(Hash.new) do |hash, line|
> id, sequence = line.split(/\s+/,2)
> # hash.class == String
> hash[id] = sequence
> end
> end


How inject works is that it passes the block two values:
an accumulator and one of the elements in the enumerable
(one at a time). In each iteration, the accumulator will be the
return value of the block in the previous iteration.

So in your example, if you want the accumulator to be a hash,
your block should return a hash. Try this (untested):

def sequences
self.alignment.split(/\n/).inject(Hash.new) do |hash, line|
id, sequence = line.split(/\s+/,2)
# hash.class == String
hash[id] = sequence
hash # <<< the change
end
end

Hope this s,

Jesus.

  Réponse avec citation
Vieux 19/06/2008, 14h46   #4
Mike Barton
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: I don't really understand inject

I see, I see, I see.
That solved the problem. Thanks for that injection!
Ha Ha Ha!

On Jun 19, 2:36pm, Jesús Gabriel y Galán <jgabrielyga...@gmail.com>
wrote:
> On Thu, Jun 19, 2008 at 3:13 PM, Mike Barton <barton.mich...@gmail.com> wrote:
> > I think these methods should both do the same thing. The second works,
> > however in the first I get errors that string is not matched, where
> > hash is an instance of String not hash

>
> > def sequences
> > self.alignment.split(/\n/).inject(Hash.new) do |hash, line|
> > id, sequence = line.split(/\s+/,2)
> > # hash.class == String
> > hash[id] = sequence
> > end
> > end

>
> How inject works is that it passes the block two values:
> an accumulator and one of the elements in the enumerable
> (one at a time). In each iteration, the accumulator will be the
> return value of the block in the previous iteration.
>
> So in your example, if you want the accumulator to be a hash,
> your block should return a hash. Try this (untested):
>
> def sequences
> self.alignment.split(/\n/).inject(Hash.new) do |hash, line|
> id, sequence = line.split(/\s+/,2)
> # hash.class == String
> hash[id] = sequence
> hash # <<< the change
> end
> end
>
> Hope this s,
>
> Jesus.


  Réponse avec citation
Vieux 19/06/2008, 15h22   #5
Robert Dober
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: I don't really understand inject

On Thu, Jun 19, 2008 at 4:08 PM, Mike Barton <barton.michael@gmail.com> wrote:
> I see, I see, I see.
> That solved the problem. Thanks for that injection!
> Ha Ha Ha!

Nice one, fortunately you did not ask for on reduce (which is an
alias of inject) .
Robert

  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 03h50.


É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,11993 seconds with 13 queries