# Add a method to the Error class

**URL:** <https://www.ruby-forum.com/t/add-a-method-to-the-error-class/48925>\
**Category:** Rails\
**Created:** [November 20, 2005, 9:19pm UTC](https://www.ruby-forum.com/t/add-a-method-to-the-error-class/48925 "2005-11-20T21:19:25Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Simon.Santoro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@Simon.Santoro](https://www.ruby-forum.com/u/Simon.Santoro)\
**Post date:** [November 20, 2005, 9:19pm UTC](https://www.ruby-forum.com/t/add-a-method-to-the-error-class/48925/1 "2005-11-20T21:19:25Z")

</div>

Hello all!  
I am trying to write a plugin, and I would like to add a method to the  
ActiveRecord::Errors class:

module Simon  
def self.included(base)  
base.extend(ClassMethods)  
end  
module ClassMethods  
def hello  
“ciao!”  
end  
end  
end

ActiveRecord::Errors.class\_eval do  
include Simon  
end

but when i try to call it from my controller:  
@stuff = @person.errors.hello

I get  
undefined method `hello’ for #ActiveRecord::Errors:0xb7888df4

Why?

---

<div class="post-metadata">

**Author:** ![Simon.Santoro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@Simon.Santoro](https://www.ruby-forum.com/u/Simon.Santoro)\
**Post date:** [November 20, 2005, 9:27pm UTC](https://www.ruby-forum.com/t/add-a-method-to-the-error-class/48925/2 "2005-11-20T21:27:32Z")

</div>

On 11/20/05, Simon S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ```
> "ciao!"
> 
> ```
> 
> I get  
> undefined method `hello’ for #ActiveRecord::Errors:0xb7888df4
> 
> Why?

Because you’re trying to add a class method to Errors and then call an  
instance method. Try putting hello() inside Simon.

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![Simon.Santoro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@Simon.Santoro](https://www.ruby-forum.com/u/Simon.Santoro)\
**Post date:** [November 20, 2005, 9:35pm UTC](https://www.ruby-forum.com/t/add-a-method-to-the-error-class/48925/3 "2005-11-20T21:35:37Z")

</div>

Rick O. wrote:

> Because you’re trying to add a class method to Errors and then call an  
> instance method. Try putting hello() inside Simon.

Thanks, it works. But I did not understand it…  
If I add the method to the class, will it not become an instance method  
once I create an instance of that class? If not, what are class\_eval,  
module\_eval, and instance\_eval supposed to do?

---

<div class="post-metadata">

**Author:** ![Simon.Santoro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@Simon.Santoro](https://www.ruby-forum.com/u/Simon.Santoro)\
**Post date:** [November 20, 2005, 10:11pm UTC](https://www.ruby-forum.com/t/add-a-method-to-the-error-class/48925/4 "2005-11-20T22:11:52Z")

</div>

> Thanks, it works. But I did not understand it…  
> If I add the method to the class, will it not become an instance method  
> once I create an instance of that class? If not, what are class\_eval,  
> module\_eval, and instance\_eval supposed to do?

class\_eval and module\_eval are aliases:  
[http://ruby-doc.org/core/classes/Module.html#M000694](http://ruby-doc.org/core/classes/Module.html#M000694)

instance\_eval:  
[http://ruby-doc.org/core/classes/Object.html#M001000](http://ruby-doc.org/core/classes/Object.html#M001000)

Anytime you define methods in a class, they’re accessed as instance  
methods. When you do:

class Foo  
def self.bar  
end  
end

You’re adding a method to the singleton class, so the method is  
available like this:

Foo.bar

Remember that Foo is just an instance object of Class.

I’d highly suggest reading the pickaxe book on this. It explains it  
much better and provides diagrams…

–  
rick  
[http://techno-weenie.net](http://techno-weenie.net)

---

<div class="post-metadata">

**Author:** ![Simon.Santoro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@Simon.Santoro](https://www.ruby-forum.com/u/Simon.Santoro)\
**Post date:** [November 20, 2005, 10:31pm UTC](https://www.ruby-forum.com/t/add-a-method-to-the-error-class/48925/5 "2005-11-20T22:31:58Z")

</div>

Rick O. wrote:

> I’d highly suggest reading the pickaxe book on this. It explains it  
> much better and provides diagrams…

Right. Thanks

---

<div class="post-metadata">

**Author:** ![Simon.Santoro](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/73ab20/32.png) [@Simon.Santoro](https://www.ruby-forum.com/u/Simon.Santoro)\
**Post date:** [November 21, 2005, 3:01am UTC](https://www.ruby-forum.com/t/add-a-method-to-the-error-class/48925/6 "2005-11-21T03:01:36Z")

</div>

Simon S. wrote:

> Rick O. wrote:
> 
> > Because you’re trying to add a class method to Errors and then call an  
> > instance method. Try putting hello() inside Simon.
> 
> Thanks, it works. But I did not understand it…  
> If I add the method to the class, will it not become an instance method  
> once I create an instance of that class? If not, what are class\_eval,  
> module\_eval, and instance\_eval supposed to do?

If you just want to add a hello method to ActiveRecord::Errors, all  
you need to do is ‘re-open’ the class in your own code and add the  
method:

class ActiveRecord::Errors

def hello  
‘hi’  
end

end

Try something similar in irb:

C:\Documents and Settings\Justin\>irb  
irb(main):001:0\> class Foo  
irb(main):002:1\> def hello  
irb(main):003:2\> ‘hi’  
irb(main):004:2\> end  
irb(main):005:1\> end  
=\> nil  
irb(main):006:0\> f = Foo.new  
=\> #Foo:0x2cfa7b8  
irb(main):007:0\> f.hello  
=\> “hi”  
irb(main):008:0\> class Foo  
irb(main):009:1\> def bye  
irb(main):010:2\> ‘ciao’  
irb(main):011:2\> end  
irb(main):012:1\> end  
=\> nil  
irb(main):013:0\> f.hello  
=\> “hi”  
irb(main):014:0\> f.bye  
=\> “ciao”  
irb(main):015:0\>

(notice that the existing instance, f, gets the new method)

regards

```
Justin
```
