{"id":1081,"date":"2022-08-30T15:12:04","date_gmt":"2022-08-30T15:12:04","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/09\/is-there-a-way-to-create-temporary-namespaces-and-constants-in-ruby-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:12:04","modified_gmt":"2022-08-30T15:12:04","slug":"is-there-a-way-to-create-temporary-namespaces-and-constants-in-ruby-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/is-there-a-way-to-create-temporary-namespaces-and-constants-in-ruby-collection-of-common-programming-errors\/","title":{"rendered":"Is there a way to create temporary namespaces and constants in Ruby?-Collection of common programming errors"},"content":{"rendered":"<p>I have a class:<\/p>\n<pre><code>class MyClass\n  def self.say_hello\n    puts \"hello\"\n  end\nend\n<\/code><\/pre>\n<p>and I want to create a process to override the class and its method temporarily:<\/p>\n<pre><code>begin \"a temporary namespace, constants, variables and methods within this code\"\n  Thread.current[:neverland] = -&gt; do\n    Object.instance_exec do\n      class MyClass\n        def self.say_hi\n          puts \"hi\"\n        end\n      end\n\n      MyClass.say_hi\n      MyClass.say_hello\n    end\n  end\nend\n\n&gt; Thread.current[:neverland].call\n=&gt; \"hi\"\n=&gt; \"hello\"\n\n&gt; MyClass.methods - Object.methods\n=&gt; [\"say_hello\"]\n\n&gt; MyClass.say_hi\n=&gt; undefined method `say_hi' for MyClass:Class (NoMethodError)\n<\/code><\/pre>\n<p>Is there something like this in Ruby or am I just dreaming? Namespace pollution-free, temporary constants, methods, namespace, class. Clean, focused and optimized code, without too much distractions.<\/p>\n<p id=\"rop\"><small>Originally posted 2013-11-09 23:22:01. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I have a class: class MyClass def self.say_hello puts &#8220;hello&#8221; end end and I want to create a process to override the class and its method temporarily: begin &#8220;a temporary namespace, constants, variables and methods within this code&#8221; Thread.current[:neverland] = -&gt; do Object.instance_exec do class MyClass def self.say_hi puts &#8220;hi&#8221; end end MyClass.say_hi MyClass.say_hello end [&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-1081","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1081","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=1081"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1081\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}