title: Yield variable scope with Ruby
body: <p>How does Ruby distinguish the codeblocks <code>|n|</code>'s in the functions yield case of "Kim" and the function call's case of "Eric"?</p>
  
  <p>Apologies if this is painfully obvious - I really don't see how exactly the <code>|n|</code> variable in the codeblock with the function(arg) call effects the functions internal yield call?</p>
  
  <pre><code>def yield_name(name)
    puts "In the method! Let's yield..."
    yield("Kim")
    puts "... now in between the yields!"
    yield(name)
    puts "Block complete! Back in the method."
  end
  </code></pre>
  
  <p><p>  </p>
  
  <pre><code>&gt;&gt;&gt; yield_name("Eric") { |n| puts "My name is #{n}." }
  </code></pre>
  
  <p><p></p>
  
  <blockquote>
  <pre><code>In the method! Let's yield...
  My name is Kim.
  ... now in between the yields!
  My name is Eric.
  Block complete! Back in the method.
  </code></pre>
  </blockquote>
  
  <p>As I understand the code block, it reads, "for each argument, put to screen "My name is #{that argument}". How does Ruby pass "Kim" into "that argument" so it prints "My name is Kim" instead of just "Kim"? Thank you.</p>
  
  <p>---------------------- EDIT</p>
  
  <p>Here's a less confusingly named example:</p>
  
  <pre><code>def double(parameter)
      yield parameter
      yield 7
  end
  </code></pre>
  
  <p><p>
  When I invoke:
  <p></p>
  
  <pre><code>&gt;&gt;&gt; double(3) { |n| puts "how? #{n * 2}" }
  </code></pre>
  
  <p><p>
  I get:
  <p></p>
  
  <blockquote>
  <pre><code>how? 6  #&lt;-- relative to function call(arg)
  how? 14 #&lt;-- relative to internal functions yield call(arg)
  </code></pre>
  </blockquote>
  
  <p>So how does Ruby know to use yield 7 in the puts statement ?</p>