can't do require 'cgi'

17 views
Skip to first unread message

felipon

unread,
Aug 25, 2010, 4:49:55 PM8/25/10
to Ramaze
Hi agian,

i want to use cgi.rb standard library, but when required in ramaze
fails to load:

ramaze1.8 console
/usr/lib/ruby/1.8/cgi.rb:279: warning: already initialized constant CR
/usr/lib/ruby/1.8/cgi.rb:282: warning: already initialized constant LF
/usr/lib/ruby/1.8/cgi.rb:285: warning: already initialized constant
EOL
/usr/lib/ruby/1.8/cgi.rb:287: warning: already initialized constant
REVISION
/usr/lib/ruby/1.8/cgi.rb:292: warning: already initialized constant
PATH_SEPARATOR
/usr/lib/ruby/1.8/cgi.rb:313: warning: already initialized constant
HTTP_STATUS
/usr/lib/ruby/1.8/cgi.rb:317: warning: already initialized constant
RFC822_DAYS
/usr/lib/ruby/1.8/cgi.rb:320: warning: already initialized constant
RFC822_MONTHS
/usr/lib/ruby/1.8/cgi.rb:774: superclass mismatch for class Cookie
(TypeError)


well, i suppose a clash of some type.

any suggestions ?

Thnxs.

Michael Fellinger

unread,
Aug 25, 2010, 8:08:30 PM8/25/10
to ram...@googlegroups.com

CGI is loaded already, try using it without requiring it again.

--
Michael Fellinger
CTO, The Rubyists, LLC

felipon

unread,
Aug 26, 2010, 10:37:48 AM8/26/10
to Ramaze
i mean the standard cgi.rb that comes with ruby.

i managed to solve my problem with this helper

module Ramaze
module Helper
module StandardCgi
eval File.read('/usr/lib/ruby/1.8/cgi.rb')
end
end
end

not fully tested but works perfectlly at the moment.

by the way, i think require 'foo' should be evaluated inside the
calling manespace, not de ROOT name space. What do you think ?

cheers.


On 25 ago, 20:08, Michael Fellinger <m.fellin...@gmail.com> wrote:

Lee Jarvis

unread,
Aug 26, 2010, 11:39:56 AM8/26/10
to Ramaze
It *IS* the standard CGI which Ramaze require's. Using eval to read
the file again is not what you want, and could cause you problems
later on. If cgi is already required then it shouldn't be able to
overwrite any constants because `require 'cgi'` should return false.

felipon

unread,
Aug 26, 2010, 12:19:34 PM8/26/10
to Ramaze
I don't think so ... look


NoMethodError at /test/form_test
undefined method `new' for Innate::Helper::CGI:Module
Ruby /home/felipe/Proyectos/Procasa/BusquedaPropiedadesWeb/
busquedaClasica/controller/main.rb: in form_test, line 116
Web GET squeeze-dev/test/form_test
Jump to:

* GET
* POST
* Cookies
* ENV

Traceback (innermost first)

* /home/felipe/Proyectos/Procasa/BusquedaPropiedadesWeb/
busquedaClasica/controller/main.rb: in form_test
109. "Message: #{session[:test]}"
110. # path
111. end
112.
113. def form_test
114.
115.
116. @cgi = CGI.new("html3")

Lars Olsson

unread,
Aug 26, 2010, 12:28:38 PM8/26/10
to Ramaze
CGI is a top level module. Please use ::CGI when referring to it from
within Ramaze. Like I said in the other thread, code like cgi
= ::CGI.new might not work due to Rack eating the ENV though.

/lasso

Dave Howell

unread,
Aug 27, 2010, 6:25:23 PM8/27/10
to ram...@googlegroups.com
> On 26 ago, 11:39, Lee Jarvis <ljjar...@gmail.com> wrote:
>> It *IS* the standard CGI which Ramaze require's. Using eval to read

On Aug 26, 2010, at 9:19 , felipon wrote:

> I don't think so ... look

Ah, the namespace thing again.

Felipon, I suggest you ask a different question...

"If Ramaze is requiring the standard CGI library, then how do I access it?"


> class MainController < Controller
> def ceegee
> CGI::escapeHTML('Some "testing&experimenting/looking"')
> end
> end
>
>
> http://localhost:7000/ceegee
>
> NoMethodError at /ceegee
> undefined method `escapeHTML' for Innate::Helper::CGI:Module

Lars tells us:


> CGI is a top level module. Please use ::CGI when referring to it from
> within Ramaze.

Sure enough,

> class MainController < Controller
> def ceegee
> ::CGI::escapeHTML('Some "testing&experimenting/looking"')
> end
> end


seems to work.


Which leads me to wonder what that means. The RDocs only speak of Rack::Handler::CGI and Innate::Helper::CGI. How many top level modules are there, and what are they?

Michael Fellinger

unread,
Aug 28, 2010, 8:05:13 AM8/28/10
to ram...@googlegroups.com

See http://doc.rubyists.com/ramaze+innate/Innate/Helper/CGI.html


>
> seems to work.
>
>
> Which leads me to wonder what that means. The RDocs only speak of Rack::Handler::CGI and Innate::Helper::CGI. How many top level modules are there, and what are they?

--

Lars Olsson

unread,
Aug 28, 2010, 9:10:00 AM8/28/10
to Ramaze
I think we are talking about different things here. First, the
*recommended* way of using methods corresponding to the methods in the
CGI module is by using the helper (Ramaze::Helpers::CGI). Whenever
using methods via the helper, the methods gets defined in the *same*
namespace as the controller. That means that you can use methods like
escape_html *without* a namespace qualifier. Methods loaded via the
helper *belongs* to the controller. You can test this with
self.respond_to?(:escape_html). And since the CGI helper is *always*
loaded, you always have escape_html available (even if it isn't
defined in the Ramaze::Controller class).

Sometimes you want to use stuff outside of Ramaze inside Ramaze. If
there isn't a helper, you can still access it from within Ramaze, but
then you have to "break out" of the Ramaze namespace.

For example, the CGI module (indirectly loadad by ramaze somewhere)
defines the rfc1123_date method. After Ramaze has loaded the CGI
module (not the helper) we can "break out" to the CGI namespace by
using ::, ie ::CGI.rfc1123_date.

Why do we need to break out? In what namespace do our controller code
live? If you try (on a default project):

html_escape(self.class.ancestors.inspect)

you will get:

[YourControllerClass, Controller, Innate::Helper::XHTML,
Innate::Helper::Layout, Ramaze::Controller, Innate::Helper::Aspect,
Innate::Helper::CGI, Innate::Helper::Flash, Innate::Helper::Link,
Innate::Helper::Render, Innate::Helper::Redirect,
Innate::Helper::SendFile, Innate::Trinity, Innate::Helper,
Innate::Node, Innate::Traited, Object, Ramaze::CoreExtensions::Object,
PP::ObjectMixin, Kernel, BasicObject]

These are the objects that will be queries whenever you try to call a
method in your controller. Since ruby uses modules for both
inheritance and mixins this looks a little messy, but the inheritance
chain looks something like this (manveru, please correct if wrong).

YourControllerClass < Controller (created by controller/init.rb) <
Ramaze::Controller < Innate::Node < Object < BasicObject

So, if you don't "break out" of the current namespace, methods
belonging to the Innate and Ramaze namespaces will be found, but those
belonging to other modules (like CGI) will not. That does not mean
those methods are inaccessible, just that you need to use :: to get to
the correct module first.

/lasso
> Seehttp://doc.rubyists.com/ramaze+innate/Innate/Helper/CGI.html
Reply all
Reply to author
Forward
0 new messages