title: Ruby Metaprogramming "Depths"
body: <p>I'm starting to learn metaprogramming in Ruby and (I think) I'm understanding how to add instance methods and variables, but only if passed in one at a time. For example, <code>test.new_value = true</code>. I'm wondering how to add an extra depth to my command with two dots <code>test.like.this</code>. For example:</p>
  
  <pre><code>class StackOverflow
    def initialize(name)
      @name = name
    end
  
    def method_missing(argument, *args)
      argument = argument.to_s
  
      if argument =~ /failing/
        puts 'FOUND FAILING'
        puts argument
        puts args[0]
        return
      end
  
      if argument =~ /this_test_is/
        puts 'FOUND THIS TEST IS'
        puts argument
        puts args[0]
        return
      end
  
      if argument =~ /this_works=/
        instance_variable_set("@#{argument.to_s.chop}", args[0])
        return
      else
        return instance_variable_get("@#{argument}").to_s
      end
    end
  end
  
  test = StackOverflow.new("post")
  test.this_works = true
  puts "IT WORKED: " + test.this_works
  test.this_test_is.failing
  </code></pre>
  
  <p>gives me the following output:</p>
  
  <pre><code>ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]
  
  IT WORKED: true
  FOUND THIS TEST IS
  this_test_is
  
  undefined method `failing' for nil:NilClass
  (repl):44:in `&lt;main&gt;'
  </code></pre>
  
  <p>What I'm looking to do is treat this as a variable and value pair. I'd like to know how to do both of these scenarios:</p>
  
  <p>A: Recognise <code>this_test_is</code> and treat it as a variable to have it store the string (or symbol is fine) <code>failing</code>.</p>
  
  <p>B: Recognise <code>failing</code> as the variable and if I see <code>this_test_is</code> then set <code>failing</code> to <code>true</code>, as opposed to <code>false</code> if I find <code>this_test_is_not</code>.</p>
  
  <p>Thank you in advance!</p>