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 think this is cool, ruby iterators and continuations
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
I think this is cool, ruby iterators and continuations

Réponse
 
LinkBack Outils de la discussion
Vieux 04/12/2007, 19h18   #1
wbsurfver@yahoo.com
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut I think this is cool, ruby iterators and continuations


=begin

This might be old hat to some, but I just figured out how to make an
iterator so that if I get some kind of failure, it will return the
element and try again on the same iteration. The code below is just a
simulation, but now that I have this example working, I know how to
code what I really want. This feature of ruby really impresses me
alot !


=end


#####################################

$fail = true

$cont = nil

def proc_tab
statement = ["first statement", "second statement",
"third statement"]

statement.each_with_index do |stat,idx|
if $cont
cont = $cont
$cont = nil
cont.call
end
# simulate a failure on index 1
callcc {|cont| $cont = cont } if idx == 1 and $fail
$fail = false if $cont
yield stat
end
end



["searchCIO","searchCRM"].each do |site|
(1..5).each do |art|
$fail = true
puts site + "->" + art.to_s
proc_tab do |stat|
puts "stat:" + stat
end
end
end

  Réponse avec citation
Vieux 05/12/2007, 10h27   #2
MonkeeSage
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: I think this is cool, ruby iterators and continuations

On Dec 4, 1:18 pm, "wbsurf...@yahoo.com" <wbsurf...@gmail.com> wrote:
> =begin
>
> This might be old hat to some, but I just figured out how to make an
> iterator so that if I get some kind of failure, it will return the
> element and try again on the same iteration. The code below is just a
> simulation, but now that I have this example working, I know how to
> code what I really want. This feature of ruby really impresses me
> alot !
>
> =end
>
> #####################################
>
> $fail = true
>
> $cont = nil
>
> def proc_tab
> statement = ["first statement", "second statement",
> "third statement"]
>
> statement.each_with_index do |stat,idx|
> if $cont
> cont = $cont
> $cont = nil
> cont.call
> end
> # simulate a failure on index 1
> callcc {|cont| $cont = cont } if idx == 1 and $fail
> $fail = false if $cont
> yield stat
> end
> end
>
> ["searchCIO","searchCRM"].each do |site|
> (1..5).each do |art|
> $fail = true
> puts site + "->" + art.to_s
> proc_tab do |stat|
> puts "stat:" + stat
> end
> end
> end


I'm not sure how useful it is to signal failure in the block of
proc_tab (seems like that would be something to go in the method body
instead; i.e., check the value and fix it/skip it/whatever before you
yield it to the block). But in any case, I don't think you actually
need continuations for this...

$fail = false

class IterError < Exception; end

def proc_tab
statement = ["first statement",
"second statement",
"third statement"]
statement.each_index { | idx |
begin
# simulate a failure on index 1
yield statement[idx]
raise IterError if idx == 1 and $fail
rescue IterError
$fail = false
retry
end
}
end

["searchCIO", "searchCRM"].each { | site |
(1..5).each { | art |
$fail = true
puts site + "->" + art.to_s
proc_tab { | stat |
puts "stat:" + stat
}
}
}

Regards,
Jordan
  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 17h33.


É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,10279 seconds with 10 queries