Ruby 1.9.3p362 ranges as conditions bug?

Hello,

  I am test this has a problem,

ruby -v
ruby 1.9.3p362 (2012-12-25 revision 38607) [x86_64-darwin12.2.1]

vs = (1..9).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

v1 = vs.select { |n| if n==3..n==6 then 1 end }
=> [3, 4, 5, 6]

v2 = vs.select { |n| if n==2..n==16 then 1 end }
=> [2, 3, 4, 5, 6, 7, 8, 9]

v3 = vs.select { |n| if n==3..n==6 then 1 end }
=> [1, 2, 3, 4, 5, 6]

v1 and v3 use some condition , return value is different.

What do you expect n==3..n==6 to do? That evaluates to a range of
booleans, depending on the value of n:

:001 > a1 = (0..9).to_a
=> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

:002 > a1.map{|n| p n; p n==3; p n==6; p n==3..n==6 }
0
false
false
false..false
1
false
false
false..false
2
false
false
false..false
3
true
false
ArgumentError: bad value for range
  from (irb):14:in `block in irb_binding'
  from (irb):14:in `map'
  from (irb):14
  from /home/tamara/.rvm/rubies/ruby-1.9.3-head/bin/irb:16:in `<main>'

I think rather you might try:

:003 > a2 = a1.select{|n| (3..6).include?(n) }
=> [3, 4, 5, 6]
:004 > a3 = a1.select{|n| (2..16).include?(n) }
=> [2, 3, 4, 5, 6, 7, 8, 9]
:005 > a2 = a1.select{|n| (3..6).include?(n) }
=> [3, 4, 5, 6]

if that is the way you want to go. Better, though, perhaps, to use slices:

:006 > a1[3..6]
=> [3, 4, 5, 6]
:007 > a1[2..16]
=> [2, 3, 4, 5, 6, 7, 8, 9]
:008 > a1[3..6]
=> [3, 4, 5, 6]

···

On Sun, Jan 6, 2013 at 9:09 PM, windwiny <windwiny.ubt@gmail.com> wrote:

vs = (1..9).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

v1 = vs.select { |n| if n==3..n==6 then 1 end }

Hi,

Hello,

  I am test this has a problem,

ruby -v
ruby 1.9.3p362 (2012-12-25 revision 38607) [x86_64-darwin12.2.1]

vs = (1..9).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

v1 = vs.select { |n| if n==3..n==6 then 1 end }
=> [3, 4, 5, 6]

v2 = vs.select { |n| if n==2..n==16 then 1 end }
=> [2, 3, 4, 5, 6, 7, 8, 9]

v3 = vs.select { |n| if n==3..n==6 then 1 end }
=> [1, 2, 3, 4, 5, 6]

v1 and v3 use some condition , return value is different.

Hi,

···

2013/1/7 windwiny <windwiny.ubt@gmail.com>:

2013/1/7 windwiny <windwiny.ubt@gmail.com>:

Run code on irb, pry has different result, write code to file, run from
file, that return same result. Maybe it is (1.9.3p362)irb's bug.

I confirmed this bug also occurred on ruby trunk.
I filed a bug issue on behalf of you.

Regards,

Park Heesob

These are so-called flip-flops, and a valid though obscure feature of Ruby. Here's a nicer test case:

irb(main):001:0> (1..100).each do |i|
irb(main):002:1* puts i if i==42..i==45
irb(main):003:1> end
42
43
44
45

windwiny, I wasn't able to replicate your results on ruby 1.9.3p0 (2011-10-30) [i386-mingw32]. v1 and v3 both gave the same (first) result.

···

On Mon, 07 Jan 2013 05:26:43 +0100, tamouse mailing lists <tamouse.lists@gmail.com> wrote:

On Sun, Jan 6, 2013 at 9:09 PM, windwiny <windwiny.ubt@gmail.com> wrote:

vs = (1..9).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

v1 = vs.select { |n| if n==3..n==6 then 1 end }

What do you expect n==3..n==6 to do? That evaluates to a range of
booleans, depending on the value of n:

Run code on irb, pry has different result, write code to file, run from
file, that return same result. Maybe it is (1.9.3p362)irb's bug.

···

2013/1/7 Matma Rex <matma.rex@gmail.com>

On Mon, 07 Jan 2013 05:26:43 +0100, tamouse mailing lists < > tamouse.lists@gmail.com> wrote:

On Sun, Jan 6, 2013 at 9:09 PM, windwiny <windwiny.ubt@gmail.com> wrote:

vs = (1..9).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

v1 = vs.select { |n| if n==3..n==6 then 1 end }

What do you expect n==3..n==6 to do? That evaluates to a range of
booleans, depending on the value of n:

These are so-called flip-flops, and a valid though obscure feature of
Ruby. Here's a nicer test case:

irb(main):001:0> (1..100).each do |i|
irb(main):002:1* puts i if i==42..i==45
irb(main):003:1> end
42
43
44
45

windwiny, I wasn't able to replicate your results on ruby 1.9.3p0
(2011-10-30) [i386-mingw32]. v1 and v3 both gave the same (first) result.

I really do not understand how this works. I can see what it produces, but why?

···

On Mon, Jan 7, 2013 at 12:01 AM, Matma Rex <matma.rex@gmail.com> wrote:

On Mon, 07 Jan 2013 05:26:43 +0100, tamouse mailing lists > <tamouse.lists@gmail.com> wrote:

On Sun, Jan 6, 2013 at 9:09 PM, windwiny <windwiny.ubt@gmail.com> wrote:

vs = (1..9).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

v1 = vs.select { |n| if n==3..n==6 then 1 end }

What do you expect n==3..n==6 to do? That evaluates to a range of
booleans, depending on the value of n:

These are so-called flip-flops, and a valid though obscure feature of Ruby.
Here's a nicer test case:

irb(main):001:0> (1..100).each do |i|
irb(main):002:1* puts i if i==42..i==45
irb(main):003:1> end
42
43
44
45

Hi,

···

2013/1/8 tamouse mailing lists <tamouse.lists@gmail.com>:

On Mon, Jan 7, 2013 at 12:01 AM, Matma Rex <matma.rex@gmail.com> wrote:

On Mon, 07 Jan 2013 05:26:43 +0100, tamouse mailing lists >> <tamouse.lists@gmail.com> wrote:

On Sun, Jan 6, 2013 at 9:09 PM, windwiny <windwiny.ubt@gmail.com> wrote:

vs = (1..9).to_a
=> [1, 2, 3, 4, 5, 6, 7, 8, 9]

v1 = vs.select { |n| if n==3..n==6 then 1 end }

What do you expect n==3..n==6 to do? That evaluates to a range of
booleans, depending on the value of n:

These are so-called flip-flops, and a valid though obscure feature of Ruby.
Here's a nicer test case:

irb(main):001:0> (1..100).each do |i|
irb(main):002:1* puts i if i==42..i==45
irb(main):003:1> end
42
43
44
45

I really do not understand how this works. I can see what it produces, but why?

You can see a detailed explanation at

Regards,

Park Heesob