From: botp Date: 2011-02-25T14:53:39+09:00 Subject: Re: why is $1 in a grep() equal to nil? On Fri, Feb 25, 2011 at 4:59 AM, 7stud -- wrote: > class Computer >    def initialize(an_id, data_source) >        @id = an_id >        @ds = data_source > >        @ds.methods.grep(/^get_(.+?)_info$/) do > >            puts "-->#{$1}<---" > >            Computer.send(:define_method, $1.to_sym) do inside this block, your code references $1 itself. but what you really want is that $1 be evaluated first before the block generation. >                puts "****" + $1 + "****"   #***NIL NIL NIL NIL > >                info = @ds.send("get_#{$1}_info".to_sym) >                price = @ds.send("get_#{$1}_price".to_sym) > >                alert = "" >                if price > 100 >                    alert = "*" >                end > >                puts "#{alert} #{info} #{price}" >             end >        end >    end try this (brute force) way just to get the idea, class Computer def initialize(an_id, data_source) @id = an_id @ds = data_source @ds.methods.grep(/^get_(.+?)_info$/) do puts "-->#{$1}<---" eval <<-EVILHERE Computer.send(:define_method, $1.to_sym) do #puts "****" + ($1) + "****" #***NIL NIL NIL NIL info = @ds.send("get_#{$1}_info".to_sym) price = @ds.send("get_#{$1}_price".to_sym) alert = "" if price > 100 alert = "*" end puts "\#{alert} \#{info} \#{price}" end EVILHERE end end end best regards -botp