Hal E. Fulton wrote:

>>>module M
>>>  foo = "bar"
>>>  define_method :function do
>>>    puts foo
>>>  end
>>>end
>>>include M
>>>
>>>function      # prints: bar

[snip]

> Correct me if I'm wrong, but isn't this simply
> because the do/end block itself is able to "see"
> the variable foo? 
> 
> The body of an ordinary method, i.e., def/end,
> looks similar to a do/end block but isn't one.

The following question is probably completely different to this thread, 
but I'm going to ask it anyway (with the caveat for readers that I've 
been playing with Ruby for, err, a week now).

I tried to do this yesterday:

   def foo(foobar)
     bar = 0;
     if foobar != nil
       bar = foobar * 4
     else
       bar = "foobar!"
     end
     bar
   end

 > foo(3) -> 0

from looking at it it seems that my problem is that the bar inside the 
if/else block is not the same bar that is declared at the start of the 
method.

how should I be doing the above?  I ended up doing this:

   def foo(foobar)
     bar =
       if foobar != nil
         bar = foobar * 4
       else
         bar = "foobar!"
       end
     bar
   end

which worked fine, but I was wondering if there was a way to do what I 
was trying initially.

cheers and thanks
dim

(ps - code written from memory, and obviously a silly example, not the 
real thing)