{"id":1865,"date":"2026-11-24T13:23:16","date_gmt":"2026-11-24T13:23:16","guid":{"rendered":"https:\/\/ruby-doc.org\/learn\/?p=1865"},"modified":"2026-11-24T13:23:17","modified_gmt":"2026-11-24T13:23:17","slug":"ruby-nameerror","status":"publish","type":"post","link":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/","title":{"rendered":"Ruby NameError: Find the Missing Name and Fix Its Scope"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">A Ruby NameError means a name is undefined or invalid where you use it. Start with the exception class, the reported name and the first relevant line of your code. Then check spelling, scope, namespaces and loading. Fix the connection between the reference and its definition instead of suppressing the exception.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">What appears to be a typo might really be a problem with scope. A variable elsewhere in the same file may be unavailable inside a method. Similarly, a class definition on disk is not usable until the program loads it. These differences determine which correction will help.<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"1600\" height=\"1200\" src=\"https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/11\/ruby-nameerror-diagnosis-guide.webp\" alt=\"Ruby NameError diagnosis guide showing spelling, local scope and constant namespace checks\" class=\"wp-image-1864\" srcset=\"https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/11\/ruby-nameerror-diagnosis-guide.webp 1600w, https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/11\/ruby-nameerror-diagnosis-guide-300x225.webp 300w, https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/11\/ruby-nameerror-diagnosis-guide-1024x768.webp 1024w, https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/11\/ruby-nameerror-diagnosis-guide-768x576.webp 768w, https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/11\/ruby-nameerror-diagnosis-guide-1536x1152.webp 1536w\" sizes=\"auto, (max-width: 1600px) 100vw, 1600px\" \/><figcaption class=\"wp-element-caption\">Follow the missing name: check spelling, then scope or namespace, and finally whether the defining code has loaded.<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Read a Ruby NameError before changing code<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">First, read the exception header. An undefined local variable or method points toward a bare name Ruby could not resolve. An uninitialized constant points toward a class, module or another constant. By contrast, a wrong constant name indicates an invalid name supplied to a reflective operation.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Next, follow the backtrace to the relevant application line. Keep the original input while investigating. Changing both the input and the spelling can remove the symptom without explaining the cause. Save the failing case before simplifying it, so you can check the eventual repair against the original problem.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">The examples ran in official Ruby 4.0.0 WASI, which identifies the tested runtime rather than the latest patch. Diagnostic formatting and suggestions can vary between environments. Therefore, these examples inspect exception properties instead of relying on identical full messages. No Rails application or native deployment was executed for this guide.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Start with spelling and the intended name<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">In the first example, the delivery charge exists, but the next reference swaps two letters. The handler displays the exception class and the misspelled name. Finally, the corrected reference prints the charge. Here, catching the exception helps demonstrate the failure; it is not an application fallback.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delivery_charge = 7\nbegin\n  delivery_chagre\nrescue NameError =&gt; error\n  p error.class\n  p error.name\nend\np delivery_charge<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>NameError\n:delivery_chagre\n7<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Compare the reported name with its definition character by character. Check singular versus plural forms, letter swaps and capitalization. Also, ask whether you meant a local variable, an instance variable or a method. Similar spellings do not make them interchangeable: Ruby looks them up differently, and they have different lifetimes.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/ruby\/did_you_mean\">The DidYouMean project<\/a> documents Ruby&#x27;s spelling suggestions for missing names and method calls. Use a suggestion as a clue. Before accepting it, however, make sure the proposed name represents the value or operation you intended. A valid nearby name can still produce the wrong result for your code.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">If the spelling was correct, stop renaming and examine the surrounding scope. First, search for the definition and determine whether execution reaches it. Then check whether its value is available at the failing location. Finding the same string elsewhere in your repository is merely the starting point.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Fix Ruby NameError across method boundaries<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Methods defined with Ruby&#x27;s ordinary method definition syntax do not capture local variables from the surrounding file. A helper immediately below an assignment can therefore cause confusion. Physical proximity does not imply shared local state. Instead, pass the required value to the helper as an argument.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here, the first method attempts to use the outer delivery charge. The second accepts that charge directly. Passing the value makes the dependency explicit to callers. It also lets you test a different charge without changing global state or depending on previous console input.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>delivery_charge = 7\ndef broken_charge\n  delivery_charge\nend\nbegin\n  broken_charge\nrescue NameError =&gt; error\n  p error.class\n  p error.name\nend\ndef charge_for(delivery_charge)\n  delivery_charge\nend\np charge_for(delivery_charge)<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>NameError\n:delivery_charge\n7<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Rather than introducing a global variable to remove the exception, choose storage consistent with your design. An argument supplies a caller&#x27;s value. An instance variable can represent an object&#x27;s state, but adopting one changes the method&#x27;s contract. You must also arrange for the appropriate initialization.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">When debugging a Ruby NameError in a console session, try a fresh process or a smaller script. Earlier commands may have created variables or loaded classes that mask a missing dependency. In particular, rerun the repaired code without those earlier commands. That checks whether the program establishes its own inputs and definitions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Check blocks and assignment order<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A block can access existing outer local variables. However, a local first introduced inside a block does not become available outside it afterward. You do not need a separate scope rule for every iterator. Identify where the name first belongs, and then look at where you read it.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Ruby also recognizes local assignments during parsing, even when they never execute. As a result, the conditional assignment below yields nil rather than an unknown-name exception. Simply resolving a name does not mean the value is useful. The program still needs a path that assigns the intended value.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>discount = 5 if false\np discount<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>nil<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">A variable containing nil is still a known local variable. Conversely, a misspelled bare reference may trigger lookup as a method because Ruby has not recognized a local assignment for it. That explains the combined wording about a variable or method. The message does not ask you to invent both; decide which kind of name your code intended to use.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Initialize clearly when a value must survive a conditional or loop. Alternatively, return the operation&#x27;s result and assign it outside. Use a default only when it makes sense for the application. Replacing every missing value with zero or a blank string could hide a different logic bug.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Resolve constants and namespaces<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">For an uninitialized constant, check the entire constant path. This example defines a parcel class within a shipping module. A reference to the top-level parcel constant fails, while the qualified reference finds the class. Thus, the repair identifies the intended namespace without moving the definition.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>module Shipping\n  class Parcel\n  end\nend\nbegin\n  Parcel\nrescue NameError =&gt; error\n  p error.class\n  p error.name\nend\np Shipping::Parcel.name<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>NameError\n:Parcel\n&quot;Shipping::Parcel&quot;<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Capitalization matters too: constants start with an uppercase letter, whereas typical local names start lowercase. If a reflective constant operation rejects an invalid name, loading another file will not fix its spelling. First distinguish an invalid name from a valid name that lacks a definition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Once the constant path looks right, determine how its defining file enters the program. An ordinary script may need an explicit require operation. If loading itself fails, investigate that file or dependency first. Otherwise, you could mistake a consequence of incomplete startup for an independent naming problem.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/guides.rubyonrails.org\/autoloading_and_reloading_constants.html\">The official Rails autoloading guide<\/a> explains the relationship between filenames, namespaces and constants. Check the constant expected from the file and the application&#x27;s autoload roots. Do not manually require application files managed by the autoloader. These are source-based Rails checks, not evidence from running a Rails application.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Separate Ruby NameError from NoMethodError<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Because NoMethodError is a subclass of NameError, rescuing the broader class catches missing-method errors too. That distinction matters during diagnosis. An explicit object can lack the requested operation even though the variable holding it exists. The next example demonstrates both the inheritance relationship and the actual exception class.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>p NoMethodError &lt; NameError\nbegin\n  &quot;parcel&quot;.missing_shipping_action\nrescue NameError =&gt; error\n  p error.class\nend<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>true\nNoMethodError<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/ruby-doc.org\/learn\/ruby-nomethoderror\/\">Our Ruby NoMethodError guide<\/a> covers receiver and method checks in more detail. Use that path when an exception identifies a missing operation on an object. For a bare unresolved name, begin with local scope and spelling. Always inspect the actual class rather than guessing from a message fragment.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Keep production rescue clauses narrow. A broad handler around an entire workflow can mask a later typo and treat it as success. If a fallback is intentional, document the expected failure and test that boundary. Otherwise, fix the lookup and allow unexpected failures to remain visible.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Try it: which total exists afterward?<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Before revealing the explanation, decide whether this snippet prints a total or reports a missing name. Then compare it with the corrected version, which establishes an outer total before entering the block. The amount stays the same; the location of the first assignment changes.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>1.times do\n  total = 12\nend\nbegin\n  p total\nrescue NameError =&gt; error\n  p error.class\nend<\/code><\/pre>\n\n\n\n<details class=\"wp-block-details is-layout-flow wp-block-details-is-layout-flow\"><summary>Reveal the scope explanation and corrected example<\/summary>\n\n\n<pre class=\"wp-block-code\"><code>NameError<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The first snippet reports NameError because its total is new inside the block. The second prints the calculated total because the block updates an existing outer local. Simply placing the first snippet earlier in a file would not make that block local available afterward.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>total = 0\n1.times do\n  total += 12\nend\np total<\/code><\/pre>\n\n\n<pre class=\"wp-block-code\"><code>12<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">For a follow-up, change the starting total and predict the new result. Then move the calculation into a method that accepts the starting amount. This ties the rules together: a block can share an existing outer local, while an ordinary method needs an explicit input.<\/p>\n\n\n<\/details>\n\n\n\n<h2 class=\"wp-block-heading\">Takeaway<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Fix a Ruby NameError by identifying where the missing name belongs and how the program should define it. Check spelling, pass method inputs explicitly, qualify constants and verify loading. Finally, rerun the original failing input and add a focused regression check. Silence alone does not prove the repair is correct.<\/p>\n\n","protected":false},"excerpt":{"rendered":"<p>Trace a Ruby NameError through spelling, local scope, namespaces and loading, with tested examples and a predict-the-output exercise.<\/p>\n","protected":false},"author":3,"featured_media":1864,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-1865","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ruby-tips"],"blocksy_meta":{"styles_descriptor":{"styles":{"desktop":"","tablet":"","mobile":""},"google_fonts":[],"version":7}},"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Ruby NameError: Find the Cause and Fix It<\/title>\n<meta name=\"description\" content=\"Fix a Ruby NameError with tested examples for spelling, method scope, blocks and constants. Trace the missing name and repair its underlying cause.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Ruby NameError: Find the Cause and Fix It\" \/>\n<meta property=\"og:description\" content=\"Fix a Ruby NameError with tested examples for spelling, method scope, blocks and constants. Trace the missing name and repair its underlying cause.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/\" \/>\n<meta property=\"og:site_name\" content=\"Ruby-Doc Learn\" \/>\n<meta property=\"article:published_time\" content=\"2026-11-24T13:23:16+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-11-24T13:23:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/11\/ruby-nameerror-diagnosis-guide.webp\" \/>\n\t<meta property=\"og:image:width\" content=\"1600\" \/>\n\t<meta property=\"og:image:height\" content=\"1200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/webp\" \/>\n<meta name=\"author\" content=\"James Britt\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"James Britt\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/ruby-nameerror\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/ruby-nameerror\\\/\"},\"author\":{\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/james-britt\\\/#james-britt\"},\"headline\":\"Ruby NameError: Find the Missing Name and Fix Its Scope\",\"datePublished\":\"2026-11-24T13:23:16+00:00\",\"dateModified\":\"2026-11-24T13:23:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/ruby-nameerror\\\/\"},\"wordCount\":1421,\"publisher\":{\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/#organization\"},\"image\":{\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/ruby-nameerror\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/ruby-nameerror-diagnosis-guide.webp\",\"articleSection\":[\"Ruby tips\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/ruby-nameerror\\\/\",\"url\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/ruby-nameerror\\\/\",\"name\":\"Ruby NameError: Find the Cause and Fix It\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/ruby-nameerror\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/ruby-nameerror\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/ruby-nameerror-diagnosis-guide.webp\",\"datePublished\":\"2026-11-24T13:23:16+00:00\",\"dateModified\":\"2026-11-24T13:23:17+00:00\",\"description\":\"Fix a Ruby NameError with tested examples for spelling, method scope, blocks and constants. Trace the missing name and repair its underlying cause.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/ruby-nameerror\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/ruby-nameerror\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/ruby-nameerror\\\/#primaryimage\",\"url\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/ruby-nameerror-diagnosis-guide.webp\",\"contentUrl\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/wp-content\\\/uploads\\\/2026\\\/11\\\/ruby-nameerror-diagnosis-guide.webp\",\"width\":1600,\"height\":1200,\"caption\":\"Follow the missing name: check spelling, then scope or namespace, and finally whether the defining code has loaded.\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/ruby-nameerror\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Ruby NameError: Find the Missing Name and Fix Its Scope\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/#website\",\"url\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/\",\"name\":\"Ruby-Doc Learn\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/#organization\",\"name\":\"Ruby-Doc Learn\",\"url\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/ruby-doc-logo-transparent.png\",\"contentUrl\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/ruby-doc-logo-transparent.png\",\"width\":1650,\"height\":305,\"caption\":\"Ruby-Doc Learn\"},\"image\":{\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/james-britt\\\/#james-britt\",\"name\":\"James Britt\",\"image\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/wp-content\\\/uploads\\\/2026\\\/09\\\/jgb_self-portrait-20140914.png\",\"description\":\"Creator and long-term maintainer of Ruby-Doc.org, founder of Neurogami, and author of practical Ruby tutorials for Ruby-Doc Learn.\",\"sameAs\":[\"https:\\\/\\\/jamesbritt.com\\\/\",\"https:\\\/\\\/neurogami.com\\\/\",\"https:\\\/\\\/www.oreilly.com\\\/pub\\\/au\\\/2595\",\"https:\\\/\\\/www.rubyevents.org\\\/profiles\\\/james-britt\"],\"url\":\"https:\\\/\\\/ruby-doc.org\\\/learn\\\/james-britt\\\/\",\"jobTitle\":\"Ruby developer and writer\",\"knowsAbout\":[\"Ruby programming language\",\"Ruby documentation\",\"JRuby\",\"Open Sound Control\",\"Software development\"],\"affiliation\":{\"@type\":\"Organization\",\"name\":\"Ruby-Doc.org\",\"url\":\"https:\\\/\\\/ruby-doc.org\\\/\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Ruby NameError: Find the Cause and Fix It","description":"Fix a Ruby NameError with tested examples for spelling, method scope, blocks and constants. Trace the missing name and repair its underlying cause.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/","og_locale":"en_US","og_type":"article","og_title":"Ruby NameError: Find the Cause and Fix It","og_description":"Fix a Ruby NameError with tested examples for spelling, method scope, blocks and constants. Trace the missing name and repair its underlying cause.","og_url":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/","og_site_name":"Ruby-Doc Learn","article_published_time":"2026-11-24T13:23:16+00:00","article_modified_time":"2026-11-24T13:23:17+00:00","og_image":[{"width":1600,"height":1200,"url":"https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/11\/ruby-nameerror-diagnosis-guide.webp","type":"image\/webp"}],"author":"James Britt","twitter_card":"summary_large_image","twitter_misc":{"Written by":"James Britt","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/#article","isPartOf":{"@id":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/"},"author":{"@id":"https:\/\/ruby-doc.org\/learn\/james-britt\/#james-britt"},"headline":"Ruby NameError: Find the Missing Name and Fix Its Scope","datePublished":"2026-11-24T13:23:16+00:00","dateModified":"2026-11-24T13:23:17+00:00","mainEntityOfPage":{"@id":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/"},"wordCount":1421,"publisher":{"@id":"https:\/\/ruby-doc.org\/learn\/#organization"},"image":{"@id":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/#primaryimage"},"thumbnailUrl":"https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/11\/ruby-nameerror-diagnosis-guide.webp","articleSection":["Ruby tips"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/","url":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/","name":"Ruby NameError: Find the Cause and Fix It","isPartOf":{"@id":"https:\/\/ruby-doc.org\/learn\/#website"},"primaryImageOfPage":{"@id":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/#primaryimage"},"image":{"@id":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/#primaryimage"},"thumbnailUrl":"https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/11\/ruby-nameerror-diagnosis-guide.webp","datePublished":"2026-11-24T13:23:16+00:00","dateModified":"2026-11-24T13:23:17+00:00","description":"Fix a Ruby NameError with tested examples for spelling, method scope, blocks and constants. Trace the missing name and repair its underlying cause.","breadcrumb":{"@id":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/#primaryimage","url":"https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/11\/ruby-nameerror-diagnosis-guide.webp","contentUrl":"https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/11\/ruby-nameerror-diagnosis-guide.webp","width":1600,"height":1200,"caption":"Follow the missing name: check spelling, then scope or namespace, and finally whether the defining code has loaded."},{"@type":"BreadcrumbList","@id":"https:\/\/ruby-doc.org\/learn\/ruby-nameerror\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/ruby-doc.org\/learn\/"},{"@type":"ListItem","position":2,"name":"Ruby NameError: Find the Missing Name and Fix Its Scope"}]},{"@type":"WebSite","@id":"https:\/\/ruby-doc.org\/learn\/#website","url":"https:\/\/ruby-doc.org\/learn\/","name":"Ruby-Doc Learn","description":"","publisher":{"@id":"https:\/\/ruby-doc.org\/learn\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/ruby-doc.org\/learn\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/ruby-doc.org\/learn\/#organization","name":"Ruby-Doc Learn","url":"https:\/\/ruby-doc.org\/learn\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/ruby-doc.org\/learn\/#\/schema\/logo\/image\/","url":"https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/09\/ruby-doc-logo-transparent.png","contentUrl":"https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/09\/ruby-doc-logo-transparent.png","width":1650,"height":305,"caption":"Ruby-Doc Learn"},"image":{"@id":"https:\/\/ruby-doc.org\/learn\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/ruby-doc.org\/learn\/james-britt\/#james-britt","name":"James Britt","image":"https:\/\/ruby-doc.org\/learn\/wp-content\/uploads\/2026\/09\/jgb_self-portrait-20140914.png","description":"Creator and long-term maintainer of Ruby-Doc.org, founder of Neurogami, and author of practical Ruby tutorials for Ruby-Doc Learn.","sameAs":["https:\/\/jamesbritt.com\/","https:\/\/neurogami.com\/","https:\/\/www.oreilly.com\/pub\/au\/2595","https:\/\/www.rubyevents.org\/profiles\/james-britt"],"url":"https:\/\/ruby-doc.org\/learn\/james-britt\/","jobTitle":"Ruby developer and writer","knowsAbout":["Ruby programming language","Ruby documentation","JRuby","Open Sound Control","Software development"],"affiliation":{"@type":"Organization","name":"Ruby-Doc.org","url":"https:\/\/ruby-doc.org\/"}}]}},"_links":{"self":[{"href":"https:\/\/ruby-doc.org\/learn\/wp-json\/wp\/v2\/posts\/1865","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ruby-doc.org\/learn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ruby-doc.org\/learn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ruby-doc.org\/learn\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/ruby-doc.org\/learn\/wp-json\/wp\/v2\/comments?post=1865"}],"version-history":[{"count":1,"href":"https:\/\/ruby-doc.org\/learn\/wp-json\/wp\/v2\/posts\/1865\/revisions"}],"predecessor-version":[{"id":1866,"href":"https:\/\/ruby-doc.org\/learn\/wp-json\/wp\/v2\/posts\/1865\/revisions\/1866"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/ruby-doc.org\/learn\/wp-json\/wp\/v2\/media\/1864"}],"wp:attachment":[{"href":"https:\/\/ruby-doc.org\/learn\/wp-json\/wp\/v2\/media?parent=1865"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ruby-doc.org\/learn\/wp-json\/wp\/v2\/categories?post=1865"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ruby-doc.org\/learn\/wp-json\/wp\/v2\/tags?post=1865"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}