[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

eruby: problem defining a new method for Array

Davi Barbosa

10/2/2008 2:19:00 PM

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:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd...
<html xmlns="http://www.w3.org/1999/x... xml:lang="en">
<head>
<title>error</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
</head>

<body>

<%
class Array
def to_hash
{'test' => 'ing'}
end
end

print [1,2,3].to_hash
%>
</body>


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
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd...
[omitted output]
<body>

testing
</body>


Anyone have any idea?
Also, someone knows where to ask? I searched for a eruby mailing list or
forum but I did not find.
--
Posted via http://www.ruby-....

4 Answers

Calamitas

10/2/2008 8:24:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Thu, Oct 2, 2008 at 4:19 PM, Davi Barbosa
<d.barbosa+ruby@gmail.com<d.barbosa%2Bruby@gmail.com>
> wrote:

> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
> "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd...
> <html xmlns="http://www.w3.org/1999/x... xml:lang="en">
> <head>
> <title>error</title>
> <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
> </head>
>
> <body>
>
> <%
> class Array


class ::Array


>
> def to_hash
> {'test' => 'ing'}
> end
> end
>
> print [1,2,3].to_hash
> %>
> </body>
>

Peter

Davi Barbosa

10/2/2008 9:14:00 PM

0

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.
--
Posted via http://www.ruby-....

Calamitas

10/2/2008 9:56:00 PM

0

[Note: parts of this message were removed to make it a legal post.]

On Thu, Oct 2, 2008 at 11:14 PM, Davi Barbosa
<d.barbosa+ruby@gmail.com<d.barbosa%2Bruby@gmail.com>
> 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

Davi Barbosa

10/3/2008 9:29:00 AM

0

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 :)
--
Posted via http://www.ruby-....