Hi --

On Thu, 2 Mar 2006, cremes.devlist / mac.com wrote:

> I was intrigued by the post that _why put up a few days/weeks ago about using 
> case/when for testing arrays [1]. I thought I'd give it a try but I can't 
> make it work. What am I doing wrong?
>
> irb(main):001:0> r1 = (0..12).to_a
> => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
> irb(main):002:0> r2 = (20..32).to_a
> => [20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32]
> irb(main):003:0> def foo range
> irb(main):004:1>  case range
> irb(main):005:2> when *r1
> irb(main):006:2>   puts "#{range} in r1"
> irb(main):007:2> when *r2
> irb(main):008:2>   puts "#{range} in r2"
> irb(main):009:2> else
> irb(main):010:2*   puts "#{range} NOT in any range"
> irb(main):011:2> end
> irb(main):012:1> end
> => nil
> irb(main):013:0> foo 3
> NameError: undefined local variable or method `r1' for main:Object
>       from (irb):5:in `foo'
>       from (irb):13

You've defined r1 and r2 outside of the scope of the method definition
block.  You need:

   def foo(range)
     r1 = (0..12).to_a
     r2 = .... etc

By the way you can also just use the ranges to test inclusion.



David

-- 
David A. Black (dblack / wobblini.net)
Ruby Power and Light (http://www.rubypowerandlight.com)

"Ruby for Rails" chapters now available
from Manning Early Access Program! http://www.manning.com/books/black