title: Ruby catching NameError
body: <p>I have the following code with two classes</p>
  
  <pre><code>class DeliveryService
    attr_reader :cities
  
    def initialize *cities 
      @cities = cities
    end
  end
  
  class Product
    attr_accessor :name
  
    def initialize name
      @name = name
    end
  
    def deliver_to delivery_service, city
      if delivery_service.cities.include?(city)
        puts "Product has been delivered"
      else
        puts "Сontact another delivery service"
      end
    end
  end
  </code></pre>
  
  <p>I want to <code>deliver_to</code> method throw something like "Choose valid delivery service" when invalid <code>delivery_service</code> provided (which doesn't exist), but instead I get NameError</p>
  
  <p>I tried to put this in different places in my code</p>
  
  <pre><code>rescue NameError
    puts "Choose valid delivery service"
  </code></pre>
  
  <p>but it doesn't work</p>
  
  <pre><code>irb(main):001:0&gt; require './DeliveryService.rb'
  =&gt; true
  irb(main):002:0&gt; fedex = DeliveryService.new "Moscow", "Saint-Petersburg"
  =&gt; #&lt;DeliveryService:0x007f66ded31520 @cities=["Moscow", "Saint-Petersburg"]&gt;
  irb(main):003:0&gt; product = Product.new "mug"
  =&gt; #&lt;Product:0x007f66ded1e498 @name="mug"&gt;
  irb(main):004:0&gt; product.deliver_to fedex, "Moscow"
  Product has been delivered
  =&gt; nil
  irb(main):005:0&gt; product.deliver_to dhl, "Moscow"
  NameError: undefined local variable or method `dhl' for main:Object
      from (irb):6
      from /home/budkin/.rbenv/versions/2.1.2/bin/irb:11:in `&lt;main&gt;'
  irb(main):006:0&gt; 
  </code></pre>