From: "Esteban Manchado Velázquez" Date: 2005-07-04T01:34:29+09:00 Subject: Re: FlipFlop Voodoo On Sun, Jul 03, 2005 at 01:44:32PM +0900, Daniel Nugent wrote: > So I was reading through the retrospective on RedHanded today and saw > the piece of flipflops > (http://redhanded.hobix.com/inspect/hopscotchingArraysWithFlipFlops.html). > > Reading it I realized that I didn't quite understand how the > flipfloperator worked exactly. So I grabbed the Pickaxe2 and read > that it returns true when the state machine is in the set state at the > end of the call. > > Looking at both why's examples, the ones in Pickaxe2, and through a > little experimentation of my own and once again reviewing the state > transition diagram in Pickaxe2, I figured there must be a typo. > > See example 1 on pg 342: > > a = (11..20).collect {|i| (i%4 == 0)..(i%3 == 0) ? i ? nil} ^ Typo here ------------------------------------------' Should be ":", of course. It's unrelated to what you're asking, anyway ;-) > a -> [nil, 12, nil, nil, nil, 16, 17, 18, nil, 20] > [...] > However, it does not explain why this happens or why it also outputs > 18 even though 18%3==0 should evaluate to true and push the state > machine into the unset state. The "problem" here is that two dot ranges _include_ the ending. What I don't quite understand is this :-) --------------------------------- 8< --------------------------------- zoso@velutha:~/tmp$ cat >number_list one two three four five zoso@velutha:~/tmp$ irb irb(main):001:0> File.foreach('number_list') do |line| irb(main):002:1* if line =~ /two/ .. line =~ /four/ irb(main):003:2> puts "#{line.chomp} is included in the two..four range" irb(main):004:2> end irb(main):005:1> if line =~ /two/ ... line =~ /four/ irb(main):006:2> puts "#{line.chomp} is included in the two...four range" irb(main):007:2> end irb(main):008:1> end two is included in the two..four range two is included in the two...four range three is included in the two..four range three is included in the two...four range four is included in the two..four range four is included in the two...four range => nil irb(main):009:0> (1..20).to_a => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20] irb(main):010:0> (1...20).to_a => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19] --------------------------------- >8 --------------------------------- It seems that ranges in conditions always behave as "..", even if you use the "..." operator. Another, simpler, example: --------------------------------- 8< --------------------------------- irb(main):007:0> (1..20).each do |i| irb(main):008:1* puts i if (i == 4 .. i == 5) irb(main):009:1> end 4 5 => 1..20 irb(main):010:0> (1..20).each do |i| irb(main):011:1* puts i if (i == 4 ... i == 5) irb(main):012:1> end 4 5 => 1..20 --------------------------------- >8 --------------------------------- Is there any reason for this? Is it a bug? -- Esteban Manchado Vel�zquez - http://www.foton.es EuropeSwPatentFree - http://EuropeSwPatentFree.hispalinux.es