Even my pickaxe book suggests this should work but it doesn''t... for tax in @taxes next if ! (tax.taxauthid === [ 24, 25, 26, 27, 36, 37, 38, 39]) print tax.taxamount + " - " + tax.taxauthid + "\n" end and my thinking is that if the tax.taxauthid is not found in the set of values, it should jump to the next value of tax. Why doesn''t this work? Craig -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
2009/7/29 Craig White <craigwhite-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org>:> > Even my pickaxe book suggests this should work but it doesn''t... > > for tax in @taxes > next if ! (tax.taxauthid === [ 24, 25, 26, 27, 36, 37, 38, 39]) > print tax.taxamount + " - " + tax.taxauthid + "\n" > end > > and my thinking is that if the tax.taxauthid is not found in the set of > values, it should jump to the next value of tax. >Have you tried replacing the ''next'' with another print statement to see whether it is the logic or the test that is failing? Do I see a space after the ! ? I have not tried it but Ruby can sometimes be a bit funny with spaces. Colin
> 2009/7/29 Craig White <craigwhite-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org>: >> >> Even my pickaxe book suggests this should work but it doesn''t... >> >> for tax in @taxes >> next if ! (tax.taxauthid === [ 24, 25, 26, 27, 36, 37, 38, 39]) >> print tax.taxamount + " - " + tax.taxauthid + "\n" >> endHello Craig, Why don''t you use the include? method of Array instead, and get rid of the negation by using unless. It will make the code easier to read: next unless [ 24, 25, 26, 27, 36, 37, 38, 39].include?(tax.taxauthid) Franz
I am not familiar with ''triple equals'' what is the difference between === and = If I were doing the same thing I''d write it @taxes.each do |tax| for tax in @taxes next if ! (tax.taxauthid === [ 24, 25, 26, 27, 36, 37, 38, 39]) print tax.taxamount + " - " + tax.taxauthid + "\n" end On Jul 29, 12:36 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2009/7/29 Craig White <craigwh...-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org>: > > > > > Even my pickaxe book suggests this should work but it doesn''t... > > > for tax in @taxes > > next if ! (tax.taxauthid === [ 24, 25, 26, 27, 36, 37, 38, 39]) > > print tax.taxamount + " - " + tax.taxauthid + "\n" > > end > > > and my thinking is that if the tax.taxauthid is not found in the set of > > values, it should jump to the next value of tax. > > Have you tried replacing the ''next'' with another print statement to > see whether it is the logic or the test that is failing? Do I see a > space after the ! ? I have not tried it but Ruby can sometimes be a > bit funny with spaces. > > Colin
I may be posting this twice, sorry if thats the case... Question, what is the difference between === and = If I were writing this I would write it with the member? method of array and avoid the next statement and the negation to make it as readable as possible. @taxes.each do |tax| if [24, 25, 26, 27, 36, 37, 38, 39].member? tax.taxauthid print "#{tax.taxamount}-#{tax.taxauthid}\n" end end On Jul 29, 12:50 am, Franz Strebel <franz.stre...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 2009/7/29 Craig White <craigwh...-BQ75lA0ptkhBDgjK7y7TUQ@public.gmane.org>: > > >> Even my pickaxe book suggests this should work but it doesn''t... > > >> for tax in @taxes > >> next if ! (tax.taxauthid === [ 24, 25, 26, 27, 36, 37, 38, 39]) > >> print tax.taxamount + " - " + tax.taxauthid + "\n" > >> end > > Hello Craig, > > Why don''t you use the include? method of Array instead, and get > rid of the negation by using unless. It will make the code easier to > read: > > next unless [ 24, 25, 26, 27, 36, 37, 38, 39].include?(tax.taxauthid) > > Franz
difficult to provide anything more ugly> for tax in @taxes > next if ! (tax.taxauthid === [ 24, 25, 26, 27, 36, 37, 38, 39]) > print tax.taxamount + " - " + tax.taxauthid + "\n" > end
If I understand correctly, you''d like to output some information about a selected group of taxes. How about something like: selected = @taxes.select { |tax| [24, 25, 26, 27, 36, 37, 38, 39].include? tax.taxauthid } selected.each { |tax| puts "#{tax.taxamount} - #{tax.taxauthid}" } Bryan
In most cases that I see (according to the Pickaxe) this is "Case Equality" that is just a synonym to ==. On Jul 29, 3:00 am, Josh <joshbwh...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I may be posting this twice, sorry if thats the case... > > Question, what is the difference between === and =>
Craig White wrote:> Even my pickaxe book suggests this should work but it doesn''t... > > for tax in @taxes > next if ! (tax.taxauthid === [ 24, 25, 26, 27, 36, 37, 38, 39]) > print tax.taxamount + " - " + tax.taxauthid + "\n" > end > > and my thinking is that if the tax.taxauthid is not found in the set of > values, it should jump to the next value of tax. > > Why doesn''t this work?Well, if you''re trying to use Array#===, then the array needs to be on the *left* side of the operator. But I agree that this is an ugly way of doing things. Probably better is: @taxes.select{|tax| [24, 25, 26, ...].include? tax.taxauthid}.each do |tax| # print stuff end Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
On Wed, 2009-07-29 at 17:14 +0200, Marnen Laibow-Koser wrote:> Craig White wrote: > > Even my pickaxe book suggests this should work but it doesn''t... > > > > for tax in @taxes > > next if ! (tax.taxauthid === [ 24, 25, 26, 27, 36, 37, 38, 39]) > > print tax.taxamount + " - " + tax.taxauthid + "\n" > > end > > > > and my thinking is that if the tax.taxauthid is not found in the set of > > values, it should jump to the next value of tax. > > > > Why doesn''t this work? > > Well, if you''re trying to use Array#===, then the array needs to be on > the *left* side of the operator. But I agree that this is an ugly way > of doing things. Probably better is: > > @taxes.select{|tax| [24, 25, 26, ...].include? tax.taxauthid}.each do > |tax| > # print stuff > end---- yes, thanks...the .include? worked where the comparison operator === did not work for me (or at least I gave up trying) Craig -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
Craig White wrote: [...]> yes, thanks...the .include? workedGreat.> where the comparison operator === did > not work for me (or at least I gave up trying)Why do you say it still didn''t work? I explained in my previous message *exactly* how to get === to work. Please read text, not just code samples. :)> > Craig >Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
On Wed, 2009-07-29 at 19:30 +0200, Marnen Laibow-Koser wrote:> Craig White wrote: > [...] > > yes, thanks...the .include? worked > > Great. > > > where the comparison operator === did > > not work for me (or at least I gave up trying) > > Why do you say it still didn''t work? I explained in my previous message > *exactly* how to get === to work. Please read text, not just code > samples. :)---- I said I gave up trying...I think you are probably correct but I am struggling with find and ''joins'' now which are far more important to me. I was going to use this for/next loop thing but when I realized that ez-where plugin was not just returning unneeded records but all records, this became far less important for me. Craig -- This message has been scanned for viruses and dangerous content by MailScanner, and is believed to be clean.
I''d bet I''m not the only one waiting to see your beautiful code. ;p Pepe On Jul 29, 4:18 am, Rakoth <rakot...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> difficult to provide anything more ugly > > > for tax in @taxes > > next if ! (tax.taxauthid === [ 24, 25, 26, 27, 36, 37, 38, 39]) > > print tax.taxamount + " - " + tax.taxauthid + "\n" > > end