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