tags:

views:

89

answers:

1

I am a ruby newb and am having a problem with procs created inside loops accessing loop variables. Here is a code file demonstrating the problem.

class Route
    def initialize options = {}
    options.each do |k,v| 
      self.instance_variable_set("@#{k}", v)
      self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
      self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
     end
    end
end

routes = []
routes << Route.new({ :url=>'/one', :content=>'html1' })
routes << Route.new({ :url=>'/two', :content=>'html2' })

blocks = []
for route in routes
     blocks << Proc.new { route.content }
end

puts blocks[0].call #=> 'html2'
puts blocks[1].call #=> 'html2'

How can I make blocks[0].call return 'html1' and blocks[1].call return 'html2'

+3  A: 

You can create the Procs inside a block to provide each Proc a diferent context:

class Route
  def initialize(options = {})
    options.each do |k,v| 
      self.instance_variable_set("@#{k}", v)
      self.class.send(:define_method, k, proc{self.instance_variable_get("@#{k}")})
      self.class.send(:define_method, "#{k}=", proc{|v| self.instance_variable_set("@#{k}", v)})
    end
  end
end

routes = []
routes << Route.new({ :url=>'/one', :content=>'html1' })
routes << Route.new({ :url=>'/two', :content=>'html2' })

blocks = routes.map { |e| Proc.new { e.content } }

puts blocks[0].call #=> 'html1'
puts blocks[1].call #=> 'html2'
Pedro Henriques