# Eruby: problem defining a new method for Array

**URL:** <https://www.ruby-forum.com/t/eruby-problem-defining-a-new-method-for-array/150021>\
**Category:** Ruby\
**Created:** [October 2, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/eruby-problem-defining-a-new-method-for-array/150021 "2008-10-02T16:20:33Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/898d66/32.png) [@dmjb](https://www.ruby-forum.com/u/dmjb)\
**Post date:** [October 2, 2008, 4:20pm UTC](https://www.ruby-forum.com/t/eruby-problem-defining-a-new-method-for-array/150021/1 "2008-10-02T16:20:33Z")

</div>

Hello,  
I installed eruby yesterday and I spend all night programming…  
I’m trying to define a new method for class Array but apache2+mod\_ruby  
can’t find the method!  
A create a simple example:

error

\<%  
class Array  
def to\_hash  
{‘test’ =\> ‘ing’}  
end  
end

print [1,2,3].to\_hash  
%\>

When I try to open this page, I get this error:  
simple.rhtml:18: undefined method `to_hash' for [1, 2, 3]:Array (eval):117 /usr/lib/ruby/1.8/apache/eruby-run.rb:116:in`eval\_string\_wrap’  
/usr/lib/ruby/1.8/apache/eruby-run.rb:116:in `run' /usr/lib/ruby/1.8/apache/eruby-debug.rb:70:in`run’  
/usr/lib/ruby/1.8/apache/eruby-debug.rb:56:in `handler’

If I try the same code in a sample ruby script, it works. Even if I try  
directly with eruby it works:

$ eruby simple.rhtml

[omitted output]

testing

Anyone have any idea?  
Also, someone knows where to ask? I searched for a eruby mailing list or  
forum but I did not find.

---

<div class="post-metadata">

**Author:** ![dmjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/898d66/32.png) [@dmjb](https://www.ruby-forum.com/u/dmjb)\
**Post date:** [October 2, 2008, 10:25pm UTC](https://www.ruby-forum.com/t/eruby-problem-defining-a-new-method-for-array/150021/2 "2008-10-02T22:25:58Z")

</div>

On Thu, Oct 2, 2008 at 4:19 PM, Davi Barbosa  
\<removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> wrote:

> \<%  
> class Array

class ::Array

> def to\_hash  
> {‘test’ =\> ‘ing’}  
> end  
> end
> 
> print [1,2,3].to\_hash  
> %\>

Peter

---

<div class="post-metadata">

**Author:** ![dmjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/898d66/32.png) [@dmjb](https://www.ruby-forum.com/u/dmjb)\
**Post date:** [October 2, 2008, 11:15pm UTC](https://www.ruby-forum.com/t/eruby-problem-defining-a-new-method-for-array/150021/3 "2008-10-02T23:15:21Z")

</div>

Thank you for the answer Peter, but the problem remains. Your suggestion  
works on ruby, irb, and eruby if I run directly from the shell, but when  
I try to open the page I get an “undefined method” error.

---

<div class="post-metadata">

**Author:** ![dmjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/898d66/32.png) [@dmjb](https://www.ruby-forum.com/u/dmjb)\
**Post date:** [October 2, 2008, 11:58pm UTC](https://www.ruby-forum.com/t/eruby-problem-defining-a-new-method-for-array/150021/4 "2008-10-02T23:58:44Z")

</div>

On Thu, Oct 2, 2008 at 11:14 PM, Davi Barbosa  
\<removed\_email\_address@domain.invalid[removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

> wrote:

> Thank you for the answer Peter, but the problem remains. Your suggestion  
> works on ruby, irb, and eruby if I run directly from the shell, but when  
> I try to open the page I get an “undefined method” error.

It was a long shot as I’ve never used mod\_ruby so I can’t test any fix.

Can you try this instead?

class Object::Array  
def to\_hash  
{‘test’ =\> ‘ing’}  
end  
end

Nonetheless it’s probably better style to put any definitions in a  
separate  
Ruby file and requiring it from the rhtml file.

Peter

---

<div class="post-metadata">

**Author:** ![dmjb](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/898d66/32.png) [@dmjb](https://www.ruby-forum.com/u/dmjb)\
**Post date:** [October 3, 2008, 11:30am UTC](https://www.ruby-forum.com/t/eruby-problem-defining-a-new-method-for-array/150021/5 "2008-10-03T11:30:38Z")

</div>

Thank you, now it works!

Calamitas wrote:

> Nonetheless it’s probably better style to put any definitions in a  
> separate  
> Ruby file and requiring it from the rhtml file.
> 
> Peter

I’m doing this 🙂
