# Condition avant la boucle

**URL:** <https://www.ruby-forum.com/t/condition-avant-la-boucle/103340>\
**Category:** Ruby\
**Created:** [May 4, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/condition-avant-la-boucle/103340 "2007-05-04T13:45:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bolo](https://www.ruby-forum.com/u/Bolo)\
**Post date:** [May 4, 2007, 1:45pm UTC](https://www.ruby-forum.com/t/condition-avant-la-boucle/103340/1 "2007-05-04T13:45:23Z")

</div>

Bonjour,

Pourquoi dans mon code

def index  
rtCriteria = “”  
session[:list\_search\_criteria] = Hash.new if  
session[:list\_search\_criteria] != “”

session[:list\_search\_criteria]["#{params[:criteria]}"] =  
params[:criteria\_texte] #unless params[:criteria\_texte].blank?

session[:list\_search\_criteria].each {|key,value| rtCriteria = "  
profiles.#{key} = ‘#{value}’" }

```
 if rtCriteria.blank?
   puts rtCriteria
 else
    #actions 1

```

end

La condition se lance avant la boucle

Merci

---

<div class="post-metadata">

**Author:** ![Bolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bolo](https://www.ruby-forum.com/u/Bolo)\
**Post date:** [May 4, 2007, 2:08pm UTC](https://www.ruby-forum.com/t/condition-avant-la-boucle/103340/2 "2007-05-04T14:08:26Z")

</div>

Sorry folks, I’ll just tell Bolo to take this OL or switch language!

On 5/4/07, Bolo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> params[:criteria\_texte] #unless params[:criteria\_texte].blank?
> 
> La condition se lance avant la boucle
> 
> Merci

Bolo tu es sur une liste de courriel anglais.  
Si tu hésites de écrir en Anglais tu peux m’envoyer ça sans problème.  
Si tu es alais en Anglais restes ici c’est une liste formidable.

Pour ce qui concerne ton problème j’ai un petit peu mal à comprendre  
ce que ce code devrait faire, la method ne peut pas fonctionner comme  
ça, (c’est Rails? dans ce cas la liste de courriels serait encore une  
autre).  
session et params ne sont définit nul part, mais je devine un petit peu:

Ne voudrais-tu pas écrire  
session[:list\_search\_criteria] ||= Hash.new  
ou à la limite  
session[:list\_search\_criteria] = Hash.new unless  
session[:list\_search\_criteria] && !  
session[:list\_search\_criteria].empty?

session[:list\_search\_criteria][params[:criteria].to\_s] =  
params[:criteria\_texte] #unless params[:criteria\_texte].blank?

session[:list\_search\_criteria].each {|key,value| rtCriteria = "  
profiles.#{key} = ‘#{value}’" }  
Attention rtCriteria n’a que la dernière valeur, je propose que tu ecrives  
rtCriteria \<\< “…”

if rtCriteria.blank?  
puts rtCriteria  
else  
#actions 1  
end

le puts émet toujours “”, le sais-tu bien?

N’hésites pas à précisier tes questions Off List.

## Robert

You see things; and you say Why?  
But I dream things that never were; and I say Why not?  
– George Bernard Shaw

---

<div class="post-metadata">

**Author:** ![Bolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bolo](https://www.ruby-forum.com/u/Bolo)\
**Post date:** [May 4, 2007, 8:25pm UTC](https://www.ruby-forum.com/t/condition-avant-la-boucle/103340/3 "2007-05-04T20:25:35Z")

</div>

On 4 mai, 08:07, “Robert D.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > session[:list\_search\_criteria] = Hash.new if  
> > else
> 
> ```
> session[:list_search_criteria] && ! session[:list_search_criteria].empty?
> 
> ```
> 
> puts rtCriteria
> 
> You see things; and you say Why?  
> But I dream things that never were; and I say Why not?  
> – George Bernard Shaw

Sorry I post my question in French

---

<div class="post-metadata">

**Author:** ![Bolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bolo](https://www.ruby-forum.com/u/Bolo)\
**Post date:** [May 4, 2007, 10:40pm UTC](https://www.ruby-forum.com/t/condition-avant-la-boucle/103340/4 "2007-05-04T22:40:49Z")

</div>

On 5/4/07, Bolo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

---

<div class="post-metadata">

**Author:** ![Bolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bolo](https://www.ruby-forum.com/u/Bolo)\
**Post date:** [May 4, 2007, 8:30pm UTC](https://www.ruby-forum.com/t/condition-avant-la-boucle/103340/5 "2007-05-04T20:30:35Z")

</div>

I am apologize, I wrote my question in French

My problem is why i did something like

* * *

array = {:bolo =\> “toto” , :bolo =\> “toto” }  
array.each {|key,value| rtCriteria = “#{key} = ‘#{value}’” }  
if rtCriteria.blank?  
puts “rtCriteria is ok”  
else  
puts "rtCriteria is not "ok  
end

* * *

I have always  
rtCriteria is not ok

the if method was called before the each method

It’s more clear ?

---

<div class="post-metadata">

**Author:** ![Bolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bolo](https://www.ruby-forum.com/u/Bolo)\
**Post date:** [May 4, 2007, 10:54pm UTC](https://www.ruby-forum.com/t/condition-avant-la-boucle/103340/6 "2007-05-04T22:54:50Z")

</div>

Oops sorry wrong button

On 5/4/07, Bolo [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I am apologize, I wrote my question in French
> 
> My problem is why i did something like
> 
> * * *

Assuming:

rtCriteria = “”

> array = {:bolo =\> “toto” , :bolo =\> “toto” }  
> Actually array is a hash and it has only one pair of values :bolo =\>  
> “toto”  
> array.each {|key,value| rtCriteria = “#{key} = ‘#{value}’” }  
> You will habe rtCriteria =“bolo = ‘toto’” and not blank?, but blank?  
> is not Ruby, I believe it is defined somewere in the Rails libraries.  
> In Ruby you can use rtCriteria.strip.empty? I believe.

> if rtCriteria.blank?  
> As I said above rtCriteria is not blank it’s value = “bolo = ‘toto’”  
> puts “rtCriteria is ok”  
> else  
> puts "rtCriteria is not "ok  
> end
> 
> * * *
> 
> I have always  
> rtCriteria is not ok
> 
> the if method was called before the each method  
> It was not, if it were rtCretieria might be blank indeed!
> 
> It’s more clear ?  
> Yes and now, I know what is wrong in your code but I do not know what  
> you want.  
> You probably want a string like this  
> “bolo =\> ‘toto’, robert =\> ‘titi’” for a hash.  
> In that case try this

result =“”  
{:bolo =\> ‘toto’, :robert =\> ‘titi’}.each{|k,v| result\<\< “, #{k} =  
‘#{v}’”}  
result[2…-1]

The _Ruby_ way might be to use inject though  
{:bolo =\> ‘toto’, :robert =\> ‘titi’}.inject([]){|a,(k,v)| a \<\< “#{k} =  
‘#{v}’”}.join(", ")

If it is something else you want to achieve just tell us what the  
exact result should be.

Cheers  
Robert

---

<div class="post-metadata">

**Author:** ![Bolo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/b/898d66/32.png) [@Bolo](https://www.ruby-forum.com/u/Bolo)\
**Post date:** [May 6, 2007, 4:30pm UTC](https://www.ruby-forum.com/t/condition-avant-la-boucle/103340/7 "2007-05-06T16:30:44Z")

</div>

Hello,

> Yes and now, I know what is wrong in your code but I do not know what you want.

I would like to generate a hash for the condition in my finder

a[“profiles.name”] = “bolo”  
a[“profiles.age”] = “25”

Thanks for the tips  
a.inject([]){|rtCriteria,(k,v)| rtCriteria \<\< “#{k} =’#{v}’”}.join(",  
")

irb(main):017:0\> puts rtCriteria  
profiles.age =‘25’  
profiles.band =‘bolo’  
=\> nil  
irb(main):018:0\>

The only problem i have. I would like something like that  
profiles.age =‘25’ AND profiles.band =‘bolo’

Can you help me again ?

Thanks
