Hello I have a strange problem with this code: <%unless (@students.empty? and params[:commit].nil?) %> <%="test"%> <%= render :partial => ''results'' %> <% end %> Even though in some cases @students.empty? returns false and params [:commit].nil? returns true (or the opposite), it displays the test and the render area. I put before and after the code the <%=params[:commit].nil?%> and < %=@students.empty?%> so I can see it (it shows false and true or the opposite and always it shows the render area and the test). I also tried && but it was the same problem. The only way it worked was this: <% unless @students.empty? %> <% unless params[:commit].nil?%> <%="test"%> <%= render :partial => ''results'' %> <% end %> <% end %> So why "and" - "&&" doesn''t work? I tried every combination with () or without ()... Thank you
On Oct 1, 6:00 pm, Yiannis <istoseli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello > > I have a strange problem with this code: > > <%unless (@students.empty? and params[:commit].nil?) %> > <%="test"%> > <%= render :partial => ''results'' %> > <% end %>[snip]> > The only way it worked was this: > > <% unless @students.empty? %> > <% unless params[:commit].nil?%>I think you have some misunderstanding when it comes to boolean logic - Your code that works is just fundamentally different to the first snippet you showed. unless a unless b ... end end is equivalent to if !a && !b but unless a && b ... end is equivalent to if !( a && b) which in turn is equivalent to !a || ! b (see de morgan''s laws) Fred> > <%="test"%> > <%= render :partial => ''results'' %> > > <% end %> > <% end %> > > So why "and" - "&&" doesn''t work? I tried every combination with () or > without ()... > > Thank you
Change it to || (or). Try this in IRB: puts "Render" unless(true && false) # Prints Render puts "Render" unless(true || false) # Does not print puts "Render" unless(false || true) # Does not print puts "Render" unless(true || true) # Does not print puts "Render" unless(false || false) # Prints render (which is what you want) On Oct 1, 10:00 am, Yiannis <istoseli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hello > > I have a strange problem with this code: > > <%unless (@students.empty? and params[:commit].nil?) %> > <%="test"%> > <%= render :partial => ''results'' %> > <% end %> > > Even though in some cases @students.empty? returns false and params > [:commit].nil? returns true (or the opposite), it displays the test > and the render area. > > I put before and after the code the <%=params[:commit].nil?%> and < > %...@students.empty?%> so I can see it (it shows false and true or the > opposite and always it shows the render area and the test). I also > tried && but it was the same problem. > > The only way it worked was this: > > <% unless @students.empty? %> > <% unless params[:commit].nil?%> > > <%="test"%> > <%= render :partial => ''results'' %> > > <% end %> > <% end %> > > So why "and" - "&&" doesn''t work? I tried every combination with () or > without ()... > > Thank you
Quoting Yiannis <istoselidas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Hello > > I have a strange problem with this code: > > <%unless (@students.empty? and params[:commit].nil?) %> > <%="test"%> > <%= render :partial => ''results'' %> > <% end %> >unless both conditions are true, render the partial the equivalent is: if !@students.empty? || !params[:commit].nil? I suspect you want an ''or" instead of the ''and'' in the ''unless''. I.e. unless either condition is true, render the partial. unless @students.empty? or params[:commit].nil? equivalent: if !@students.empty? and !params[:commit].nil? or: if !@students.empty? and params[:commit] so the partial will be rendered if both there are students and params[:commit] exists and is non-nil. I personally have stopped using ''unless'' with complex arguments entirely and am using it even with simple arguments less. Lookup De Morgan''s Theorem (http://en.wikipedia.org/wiki/De_Morgan''s_theorem) and tattoo it on your arm or commit it to memory. HTH, Jeffrey
Oops! Yes indeed I need an "or" instead of "and", how I missed that?? :) Thank you all for your help and the advices! On Oct 1, 8:31 pm, "Jeffrey L. Taylor" <r...-f/t7CGFWhwGcvWdFBKKxig@public.gmane.org> wrote:> Quoting Yiannis <istoseli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > Hello > > > I have a strange problem with this code: > > > <%unless (@students.empty? and params[:commit].nil?) %> > > <%="test"%> > > <%= render :partial => ''results'' %> > > <% end %> > > unless both conditions are true, render the partial > > the equivalent is: > > if !...@students.empty? || !params[:commit].nil? > > I suspect you want an ''or" instead of the ''and'' in the ''unless''. I.e. unless > either condition is true, render the partial. > > unless @students.empty? or params[:commit].nil? > > equivalent: > > if !...@students.empty? and !params[:commit].nil? > > or: > > if !...@students.empty? and params[:commit] > > so the partial will be rendered if both there are students and params[:commit] > exists and is non-nil. > > I personally have stopped using ''unless'' with complex arguments entirely and > am using it even with simple arguments less. > > Lookup De Morgan''s Theorem (http://en.wikipedia.org/wiki/De_Morgan''s_theorem) > and tattoo it on your arm or commit it to memory. > > HTH, > Jeffrey
2009/10/1 JohnDel <istoselidas-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Oops! Yes indeed I need an "or" instead of "and", how I missed > that?? :) Thank you all for your help and the advices!I think Jeffrey is right, "unless" with complex logic is just too difficult for most of us to get our brains round easily. I avoid it except in trivially simple cases. Colin
On Thu, Oct 1, 2009 at 2:52 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> I think Jeffrey is right, "unless" with complex logic is just too > difficult for most of us to get our brains round easily.You just insulted thousands of Perl programmers. But seriously.. it''s just the opposite of "if". -- Greg Donald http://destiney.com/
2009/10/1 Greg Donald <gdonald-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > On Thu, Oct 1, 2009 at 2:52 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >> I think Jeffrey is right, "unless" with complex logic is just too >> difficult for most of us to get our brains round easily. > > You just insulted thousands of Perl programmers. > > But seriously.. it''s just the opposite of "if".Easy is the opposite of Difficult :) Maybe I should not have said ''most of us'' but certainly it applies to me. There is a similar problem with variables with names like not_valid, it is too easy to overload the brain when included in a logical expression. Colin
Colin Law wrote:> 2009/10/1 Greg Donald <gdonald-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >> >> On Thu, Oct 1, 2009 at 2:52 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>> I think Jeffrey is right, "unless" with complex logic is just too >>> difficult for most of us to get our brains round easily. >> >> You just insulted thousands of Perl programmers. >> >> But seriously.. it''s just the opposite of "if". > > Easy is the opposite of Difficult :)I agree. Even in Perl, many people consider unless poor style.> > Maybe I should not have said ''most of us'' but certainly it applies to > me. There is a similar problem with variables with names like > not_valid, it is too easy to overload the brain when included in a > logical expression.Quite. Never forget to avoid omitting to eschew expressing things in anything other than non-excessively negative form. :D> > ColinBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
2009/10/2 Marnen Laibow-Koser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>:> > Colin Law wrote: >> 2009/10/1 Greg Donald <gdonald-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: >>> >>> On Thu, Oct 1, 2009 at 2:52 PM, Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote: >>>> I think Jeffrey is right, "unless" with complex logic is just too >>>> difficult for most of us to get our brains round easily. >>> >>> You just insulted thousands of Perl programmers. >>> >>> But seriously.. it''s just the opposite of "if". >> >> Easy is the opposite of Difficult :) > > I agree. Even in Perl, many people consider unless poor style. > >> >> Maybe I should not have said ''most of us'' but certainly it applies to >> me. There is a similar problem with variables with names like >> not_valid, it is too easy to overload the brain when included in a >> logical expression. > > Quite. Never forget to avoid omitting to eschew expressing things in > anything other than non-excessively negative form. :DIt took me a couple of minutes to decide, but +1 Colin