Condition avant la boucle

B

Bolo

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
 
R

Robert Dober

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

Bonjour,

Pourquoi dans mon code

def index
rtCriteria =3D ""
session[:list_search_criteria] =3D Hash.new if
session[:list_search_criteria] !=3D ""

session[:list_search_criteria]["#{params[:criteria]}"] =3D
params[:criteria_texte] #unless params[:criteria_texte].blank?

session[:list_search_criteria].each {|key,value| rtCriteria =3D "
profiles.#{key} =3D '#{value}'" }


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

La condition se lance avant la boucle


Merci
Bolo tu es sur une liste de courriel anglais.
Si tu h=E9sites de =E9crir en Anglais tu peux m'envoyer =E7a sans probl=E8m=
e.
Si tu es alais en Anglais restes ici c'est une liste formidable.

Pour ce qui concerne ton probl=E8me j'ai un petit peu mal =E0 comprendre
ce que ce code devrait faire, la method ne peut pas fonctionner comme
=E7a, (c'est Rails? dans ce cas la liste de courriels serait encore une
autre).
session et params ne sont d=E9finit nul part, mais je devine un petit peu:

Ne voudrais-tu pas =E9crire
session[:list_search_criteria] ||=3D Hash.new
ou =E0 la limite
session[:list_search_criteria] =3D Hash.new unless
session[:list_search_criteria] && ! session[:list_search_criteria].emp=
ty?

session[:list_search_criteria][params[:criteria].to_s] =3D
params[:criteria_texte] #unless params[:criteria_texte].blank?

session[:list_search_criteria].each {|key,value| rtCriteria =3D "
profiles.#{key} =3D '#{value}'" }
Attention rtCriteria n'a que la derni=E8re valeur, je propose que tu ecrive=
s
rtCriteria << "..."


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

le puts =E9met toujours "", le sais-tu bien?

N'h=E9sites pas =E0 pr=E9cisier tes questions Off List.

Robert
--=20

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

Bolo

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

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

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

Sorry I post my question in French
 
B

Bolo

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 ?
 
R

Robert Dober

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 ?
 
R

Robert Dober

Oops sorry wrong button

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
 
B

Bolo

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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top