{"id":897,"date":"2022-08-30T15:09:00","date_gmt":"2022-08-30T15:09:00","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/dynamically-set-local-variables-in-ruby-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:09:00","modified_gmt":"2022-08-30T15:09:00","slug":"dynamically-set-local-variables-in-ruby-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/dynamically-set-local-variables-in-ruby-collection-of-common-programming-errors\/","title":{"rendered":"Dynamically set local variables in Ruby-Collection of common programming errors"},"content":{"rendered":"<p>The problem here is that the block inside each_pair has a different scope. Any local variables assigned therein will only be accessible therein. For instance, this:<\/p>\n<pre><code>args = {}\nargs[:a] = 1\nargs[:b] = 2\n\nargs.each_pair do |k,v|\n  key = k.to_s\n  eval('key = v')\n  eval('puts key')\nend\n\nputs a\n<\/code><\/pre>\n<p>Produces this:<\/p>\n<pre><code>1\n2\nundefined local variable or method `a' for main:Object (NameError)\n<\/code><\/pre>\n<p>In order to get around this, you could create a local hash, assign keys to this hash, and access them there, like so:<\/p>\n<pre><code>args = {}\nargs[:a] = 1\nargs[:b] = 2\n\nlocalHash = {}\nargs.each_pair do |k,v|\n  key = k.to_s\n  localHash[key] = v\nend\n\nputs localHash['a']\nputs localHash['b']\n<\/code><\/pre>\n<p>Of course, in this example, it&#8217;s merely copying the original hash with strings for keys. I&#8217;m assuming that the actual use-case, though, is more complex.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 22:55:57. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>The problem here is that the block inside each_pair has a different scope. Any local variables assigned therein will only be accessible therein. For instance, this: args = {} args[:a] = 1 args[:b] = 2 args.each_pair do |k,v| key = k.to_s eval(&#8216;key = v&#8217;) eval(&#8216;puts key&#8217;) end puts a Produces this: 1 2 undefined local [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-897","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/897","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=897"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/897\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}