Forum: Ruby ruby 1.9.3p362 ranges as conditions bug?

Posted by windwiny (Guest)
on 2013-01-07 04:10
(Received via mailing list)
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.
Posted by tamouse mailing lists (Guest)
on 2013-01-07 05:28
(Received via mailing list)
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:

: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]
Posted by Bartosz DziewoƄski (matmarex)
on 2013-01-07 07:02
(Received via mailing list)
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.
Posted by windwiny (Guest)
on 2013-01-07 09:05
(Received via mailing list)
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>
Posted by Heesob Park (phasis)
on 2013-01-07 10:20
(Received via mailing list)
Hi,

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

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.
https://bugs.ruby-lang.org/issues/7671

Regards,

Park Heesob
Posted by tamouse mailing lists (Guest)
on 2013-01-08 13:12
(Received via mailing list)
On Mon, Jan 7, 2013 at 12:01 AM, Matma Rex <matma.rex@gmail.com> wrote:
>>
> 42
> 43
> 44
> 45
>

I really do not understand how this works. I can see what it produces, 
but why?
Posted by Heesob Park (phasis)
on 2013-01-08 14:10
(Received via mailing list)
Hi,

2013/1/8 tamouse mailing lists <tamouse.lists@gmail.com>:
>>>
>> 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
http://ruby.about.com/od/convolutedconstructions/s...

Regards,

Park Heesob
Please log in before posting. Registration is free and takes only a minute.
Existing account (Switch to SSL-encrypted connection)
NEW: Do you have a Google/GoogleMail or Yahoo account? No registration required!
Log in with Google account | Log in with Yahoo account
No account? Register here.