On Fri, Feb 25, 2011 at 2:53 PM, 7stud -- <bbxx789_05ss / yahoo.com> wrote:
> Josh Cheek wrote in post #983791:
>> On Thu, Feb 24, 2011 at 8:04 PM, 7stud -- <bbxx789_05ss / yahoo.com>
>> wrote:
>>
>>>
>>> $1 is a *global* variable, so saying it doesn't persist outside of a
>>> block doesn't make any sense.
>>>
>>>
>> It isn't actually global. I don't know the specifics, but I used to
>> worry
>> about that too, and found out later it was not necessary.
[...]
> Uh oh. =A0Someone is going to have to explain that to me. =A0$1 does not =
act
> like a regular global variable:

This is getting more confusing for me. I altered your original script by ad=
ding:

['foo'].grep(/f(.)o/)

right before your puts "-->#{$1}<---" (at the end of the definition of
initialize). Obviously that grep was successful; but surprisingly, the
value of $1 remains 'o' even outside of the invocation of initialize:

grep_test.rb:30:in `block (2 levels) in initialize': undefined method
`get_o_info' for #<DataSource:0x82ea38> (NoMethodError)
	from grep_test.rb:49:in `<main>'

The whole script:

# From question on ruby-talk 2/24/2011

class DataSource
  def get_cpu_info
    "cpu8001"
  end
  def get_cpu_price
    101
  end

  def get_mouse_info
    "mouse241"
  end
  def get_mouse_price
    40
  end
end

class Computer
  def initialize(an_id, data_source)
    @id =3D an_id
    @ds =3D data_source

    @ds.methods.grep(/^get_(.+?)_info$/) do
      puts "-->#{$1}<---"

      Computer.send(:define_method, $1.to_sym) do
        puts "****" + $1 + "****"   #***NIL NIL NIL NIL

        info =3D @ds.send("get_#{$1}_info".to_sym)
        price =3D @ds.send("get_#{$1}_price".to_sym)

        alert =3D ""
        if price > 100
          alert =3D "*"
        end

        puts "#{alert} #{info} #{price}"
      end
      puts "-->#{$1}<---"
    end
    ['foo'].grep(/f(.)o/)
    puts "-->#{$1}<---"
  end
end

comp1 =3D Computer.new(1, DataSource.new)
puts "$1: #$1"
puts comp1.mouse