# Can You Spot the Error?

*Published on April 23, 2023*

---


I know, I know... The&nbsp;`name`&nbsp;property is public and I can assign it directly using the object. This example is vastly simplified, just to make a point. Now, give it a try again. What's wrong here?

```ruby
class Blog
  attr_accessor :name

  def publish
    # some other code...
    name = 'Rails Blog'
    # some other code...
  end

  def preview
    puts "Blog Name: " + name
  end
end
```

I also asked this question on LinkedIn, and many people mentioned that it's missing a constructor, or I should assign the value to an instance variable, i.e.&nbsp;`@name`. However, you don't need a constructor here, and the&nbsp;`attr_accessor`&nbsp;method will create an instance variable called&nbsp;`@name`&nbsp;behind the scenes.

> See this excellent, in-depth answer on StackOverflow which explains how `attr_accessor` works: &nbsp;[What is&nbsp;`attr_accessor`&nbsp;in Ruby?](https://stackoverflow.com/a/4371458/5192528)

So these are not the real issues.

## What's the Problem?

To see the problem in action, let’s create a new&nbsp;`Blog`&nbsp;object and publish it, which should assign its name.

```ruby
rails_blog = Blog.new

rails_blog.publish
```

So far, so good. Now try to access the name with the&nbsp;`preview`&nbsp;method.

```ruby
rails_blog.preview

scratch.rb:11:in `+': no implicit conversion of nil into String (TypeError)
```

How’s that possible? Didn’t we just assign the value to&nbsp;`name`?&nbsp;**Why did the**&nbsp;**`name`**&nbsp;**variable not retain its value?**

## What's the Solution?

The answer is in the way Ruby handles assignments inside objects. Let’s review the&nbsp;`publish`&nbsp;method again, which tries to set the name.

```ruby
def publish
  # some other code...
  name = 'Rails Blog'
  # some other code...
end
```

In the above code,&nbsp;`name`&nbsp;is a local variable, instead of the setter method created by&nbsp;`attr_accessor`. Hence, its scope is limited to the&nbsp;`publish`&nbsp;method and no one outside it can access it.&nbsp;**The value we assign to this local variable is lost as soon as the control exits the method.**

If we were using a more traditional language like C# or JavaScript, the difference would've been clear, as we'd have used brackets to make a method call. Since Ruby is more forgiving, it thinks we're creating a new local variable and assigns the value to it.

To access the setter method, we need to use&nbsp;`self`, like this:

```ruby
def publish
  # some other code...
  self.name = 'Rails Blog'
  # some other code...
end
```

Using&nbsp;`self`&nbsp;ensures that Ruby calls&nbsp;`Blog#name`&nbsp;method, instead of creating a local variable. Typically,&nbsp;`self`&nbsp;is not needed if you're simply calling a method in the class, Ruby will implicitly call that method on the current object. However, in this example,&nbsp;`self`&nbsp;is required.

Here’s the complete working example. If I run this code, everything works as expected.

```ruby
class Blog
  attr_accessor :name

  def publish
    # some other code...
    self.name = 'Rails Blog'
    # some other code...
  end

  def preview
    puts "Blog Name: " + name
  end
end

rails_blog = Blog.new
rails_blog.publish

rails_blog.preview  # Blog Name: Rails Blog
```

Hope this was useful, and you learned something new.

