On Sun, Jan 6, 2013 at 9:09 PM, windwiny [email protected] 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 `’
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]