Condition avant la boucle

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

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

On 5/4/07, Bolo [email protected] 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

On 4 mai, 08:07, “Robert D.” [email protected] 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

On 5/4/07, Bolo [email protected] wrote:

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 ?

Oops sorry wrong button

On 5/4/07, Bolo [email protected] 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

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

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs