# Class question

**URL:** <https://www.ruby-forum.com/t/class-question/140935>\
**Category:** Ruby\
**Created:** [June 19, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/class-question/140935 "2008-06-19T22:42:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 19, 2008, 10:42pm UTC](https://www.ruby-forum.com/t/class-question/140935/1 "2008-06-19T22:42:55Z")

</div>

class Box  
attr\_reader :exceptions

def initialize

```
  exceptions = Hash.new

```

end

```
  def exceptions?
    if(!exceptions.empty?)
      puts "error"
    else
      puts "No exceptions found."
    end
  end

```

end

box = Box.new

box.exceptions?

This prints out the error: TEST.rb:11:in `exceptions?': undefined method`empty?’ for nil:NilClass (NoMethodError)  
from TEST.rb:23

Why is my exceptions hash a NilClass??

Thanks!!

---

<div class="post-metadata">

**Author:** ![jturney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/e99b99/32.png) [@jturney](https://www.ruby-forum.com/u/jturney)\
**Post date:** [June 19, 2008, 10:48pm UTC](https://www.ruby-forum.com/t/class-question/140935/2 "2008-06-19T22:48:06Z")

</div>

On Thursday 19 June 2008, Justin To wrote:

> ```
> if(!exceptions.empty?)
> 
> ```
> 
> box.exceptions?
> 
> This prints out the error: TEST.rb:11:in `exceptions?': undefined method`empty?’ for nil:NilClass (NoMethodError)  
> from TEST.rb:23
> 
> Why is my exceptions hash a NilClass??
> 
> Thanks!!

The error is in the initialize method. Writing

exceptions = Hash.new

you assign the hash to a local variable called exceptions, not to the  
instance  
variable called @exceptions, which is the variable the method generated  
by  
attr\_reader refers to (instance variables always start with @).

By the way, in the exceptions? method, you’re using the exceptions  
method.  
This isn’t wrong, but it’s usually not necessary, since exceptions? is  
an  
instance method of class Box, which means it can access the @exceptions  
instance variable directly.

I hope this helps

Stefano
