Re: Problem with classes

Andi Scharfstein <[email protected]> Wed, 10 Aug 2005 11:47:34 +0200
Newsgroups gmane.comp.apache.mod-ruby
Message-ID <[email protected]>
Hi,

> Hmm. Perhaps also wherever you create an instance of Entry you have to use
> ::Entry.

Sadly, this doesn't work either (see below).

> How about trying to make the smallest possible program which demonstrates
> the problem?

Yes, I was just working on that. The problem seems to be the
Marshaling, I've narrowed it down to the following:

  <%
  class Test
    attr_reader :test
    def initialize
      @test = 5
    end
  end
  %>

  <html>
  <body>
  <% foo = Marshal.dump(Test.new()) %>
  <p><%= Marshal.load(foo).test %></p>
  </body>
  </html>

Without explicit namespaces (i.e. as above), I get the following:

  in `dump': can't dump anonymous class #<Module:0x40748608>::Test (TypeError)

This is also the case when I try "class ::Test". However, when I try
to do something like "Marshal.dump(::Test.new())", I get this:

  uninitialized constant Test (NameError)

So I can't reference my class properly, and the code stops working.
BTW, trying to compile it from the command line doesn't work, either:

  # eruby test3.html
  test3.html:6: compile error (SyntaxError)
  test3.html:1: parse error
   class ::Test
           ^
  test3.html:6: parse error

However, if I remove the "::" and compile it with "class Test", it
yields

  <html>
  <body>
  <p>5</p>
  </body>
  </html>

as it should. So, any ideas about this?

-- 
Bye: Andi S.                          mailto:[email protected]