How do I get access to an outer variable in a closure in ruby? -


i have following dynamically created class passed xpath function of nokogiri:

country = nil  ret = parent.xpath(".//text()[regex(.)]", class.new{   def regex(node_set, lead)     result = node_set.find_all |node|       node.text =~ post_code_expression || node.text =~ zip_code_expression     end     result   end }.new) 

i somehow access or set country variable or access outer self within regex function.

is there anyway can pass outer self class.new expression or can suggest better way?

methods cannot closures in ruby, blocks can:

country = nil  ret = parent.xpath(".//text()[regex(.)]", class.new{   define_method(:regex) |node_set, lead|     result = node_set.find_all |node|       node.text =~ post_code_expression || node.text =~ zip_code_expression     end     result   end }.new) 

by way: regex method more complicated needs be. it's equivalent to

define_method(:regex) |node_set, lead|   node_set.find_all |node|     node.text =~ post_code_expression || node.text =~ zip_code_expression   end end 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

aspxgridview - Devexpress grid - header filter does not work if column is initially hidden -

c# - How to execute a particular part of code asynchronously in a class -