Puzzling...

K

Kent S.

Your simple code is being executed in the context of some private Object
instance.

$ irb
irb(main):001:0> self.class
=> Object
irb(main):002:0>

Object class includes Kernel module which in turn defines all these
puts, print, etc. methods. Your x variable is the local variable. All
methods defined at the top level context become private methods of this
private Object.

/kent
 
G

Gavin Sinclair

Ruby beginner here. Heard of it before and on a whim decided to check
it out a couple of days ago and I love it. I friggin love it.
I'm doing stuff that would take me a while to figure out with other
languages' syntax. I've never seen a language that looks so "clean."
Anyway, I have some questions I couldn't find in the FAQ
sample code:
x = 2
puts x
Now, is "puts" a class or instance method of Object? What about "x"?
Is that a class or instance variable of an Object?
When you run a Ruby program, is the program an instance of Object?

Welcome.

x is a local variable, as opposed to @x, @@x (instance and class), X
(constant), $x (global).

puts is a method of the Kernel module, which is included in the Object
class, and therefore available anywhere.

The context of your program is the "top-level", which is an instance
of Object, and probably has some special features surrouding it.

Take a look at the standard textbook "Programming Ruby" for more juicy
goodness, although it seems you'll be able to pick up a fair bit on
your own :)

http://phrogz.net/ProgrammingRuby

Cheers,
Gavin
 
T

Trey Campbell

Ruby said:
I've tried both using \n\n and \r\n\r\n on Perl and Ruby as well. Both
resulted the same.

When I tried it with Mozilla, the "Content-type: text/plain" works. Do you
guys think it is the IE things? Thanks
I doubt that it's an IE vs. Mozilla. This is just basic HTTP stuff. The
RFC specifies that each message header end with a CRLF, with a "line"
containing only a CRLF separating the headers from the message body. So
your Content-type header, ending with two (or more) such sequences
should work; the first to terminate the header the second to separate
the header and body, and any subsequent ones are treated as part of the
body.

print "Content-type: text/html\r\n\r\n"
print "Your body goes here..."

Should and, at least in my experience, does work.

The RFC also admonishes applications to be tolerant and accept slightly
incorrectly formed documents:

"The line terminator for message-header fields is the sequence CRLF.
However, we recommend that applications, when parsing such headers,
recognize a single LF as a line terminator and ignore the leading CR."

(from http://www.w3.org/Protocols/rfc2616/rfc2616-sec19.html#sec19.3)

The following works for me:

#!C:/ruby/bin/ruby.exe
print "Content-type: text/html\n\n"
print "1st line "
print "2nd line "
print "3rd line "

I can replace the first line with one ending in \r\n\r\n (what I had
originally because it's correct, not just tolerated by well-behaved
applications). I can also replace with \n\n\n (or \r\n\r\n\r\n) which
generates an extra blank line in the document.

BTW, I'm running on WinXP SP1 with Apache 2.0.47 and Ruby 1.8.0. I
tested with IE 6.0 and FireFox 0.8. The directory that contains the
scripts is set up as follows in httpd.conf:

<Directory "C:/Apache2/htdocs/ruby">
Options Indexes FollowSymLinks ExecCGI
Order allow,deny
Allow from all
</Directory>

Bottom line, what you posted *should* work. It seems like something else
must be going on (that is, the problem's not in your Ruby script itself).

Cheers,
Trey
 
M

Mark Hubbart

Hi Osuka, yes you are right. I'm using IE6. I'm still puzzled.
<snip>
PS: I downloaded Mozilla and test it. It all work out! So, what's the
deal
with IE6?

If I understand right, when IE gets a text/plain file, It assumes it's
not really plaintext, and tries to open it as if you had double-clicked
the file. This would be peculiar to IEWin, I believe.

Now, I don't have anything running windows around here, so I can't test
this solution, but:

When I installed mod_ruby with my Apace installation, the Apache config
code that I snipped from a webpage gave ruby executables a ".rbx"
extension rather than ".rb". Perhaps if you change your httpd.conf to
accept both extensions... then rename your text/plain example to use
the ".rbx" extension. I suspect that that would solve the problem.

Good luck!

-Mark
 
R

Ruby Tuesday

One way around the problem is to change the extension from .rb to
cgi or remove the extension. Apache2 will execute the script based on the
shebang line.
Well, the same problem appears with Perl(.pl) as well. Anyone has the same
issues. The header "Content-type: text/plain \r\n\r\n" gives me so much
grief in IE6.
mod_ruby works only on Apache 1.x
I'd love to run mod_ruby which is running in my Linux Redhat 9 but now I
have to use the windows server, I'm stuck --- can't find mod_ruby/eruby
binaries for mswin32 version. Anyone compiled that already? Please share.
Thanks
 
R

Ruby Tuesday

Hi Osuka, unfortunately I have no say in what browser to use - I'm stuck
with IE and MS-Windoze.

Reading from the reply, it seems that IE6 has a little bug with header
text/plain. Have anyone encounter that issues? Try to use text/plain and put
some reqular text afterward. It works fine with Mozilla but not with IE.
Thanks
 
R

Ruby Tuesday

The following works for me:
#!C:/ruby/bin/ruby.exe
print "Content-type: text/html\n\n" <<<<<
print "1st line "
print "2nd line "
print "3rd line "

The above works for me as well on both IE6 and Mozilla. But try to change
the header to the following:
#!C:/ruby/bin/ruby.exe
print "Content-type: text/plain\n\n" <<<<<<<
print "1st line "
print "2nd line "
print "3rd line "

Again, both method works with Mozilla. So, I am begining to think that IE is
mis-behaving! Unfortunately, this is the basic RFC for header.
Please confirm.

Thanks
 
T

Trey Campbell

Ruby said:
The above works for me as well on both IE6 and Mozilla. But try to change
the header to the following:




Again, both method works with Mozilla. So, I am begining to think that IE is
mis-behaving! Unfortunately, this is the basic RFC for header.
Please confirm.

Thanks
Trying it with the following:

#!C:/ruby/bin/ruby.exe
print "Content-type: text/plain\n\n"
print "1st line "
print "2nd line "
print "3rd line "

Mozilla renders the page (as if it encountered a <pre> tag). IE shows a
dialog offering to run the associated application (but warning that it
might not be a good idea to do so). I wouldn't characterize that as
misbehaving since that's what I'd expect it to do, or at least offer to
do; it's just doing a better job of recognizing potential helper
applications than Mozilla is.

Just change the extension to something that's not associated with a
program (like .rbx) and it works the same as Mozilla. I used the name
puzzle.rbx and got the same results in both browsers. Shouldn't be a
problem if you're using the shebang line to run it as a CGI, not via the
extension.

Cheers,
Trey
 
O

Osuka Adartse

Ruby said:
Hi Osuka, unfortunately I have no say in what browser to use - I'm stuck
with IE and MS-Windoze.

Reading from the reply, it seems that IE6 has a little bug with header
text/plain. Have anyone encounter that issues? Try to use text/plain and put
some reqular text afterward. It works fine with Mozilla but not with IE.
Thanks
Hi!
Others have pointed out that changing the extension would work, so...

added to apache conf

AddType application/x-httpd-ruby .cgi
Action application/x-httpd-ruby "e:/usr/local/bin/ruby.exe"
AddType application/x-httpd-cgi .cgi

changing extensions to "cgi" or anything windows doesn't know about
should do the trick.

*-*-*-*
Other possibility, is by using an .htaccess(but depends on what a
htaccess can override and where), based on Tdiary install instructions,
*which uses* *.rb and works flawlesly in IE.

#Added this to Apache conf since windows doesn't really
#want to create files with no "filename"
AccessFileName dot.htaccess

#create a dot.access file in the dir where your're using cgi
#and add the lines:

Options +ExecCGI
#notice that Indexes, You might not want that.
Options ExecCGI Indexes
AddHandler cgi-script .ruby
#and renamed the files to *.ruby, I used a ruby ext. but whatever goes
#as long as windows doesn't knows about it(it can't use .rb, IE does the
dialogs.)

I'm still don't know why Tdiary can use the *.rb files without IE goin'
dialog mmh, btw all the files keep the shebang.

Sorry to take a time replying, but... is(was) weekend.
 
M

Mark Hubbart

Trying it with the following:

#!C:/ruby/bin/ruby.exe
print "Content-type: text/plain\n\n"
print "1st line "
print "2nd line "
print "3rd line "

Mozilla renders the page (as if it encountered a <pre> tag). IE shows
a dialog offering to run the associated application (but warning that
it might not be a good idea to do so). I wouldn't characterize that as
misbehaving since that's what I'd expect it to do, or at least offer
to do; it's just doing a better job of recognizing potential helper
applications than Mozilla is.

The mime type is there for a reason. When a file is downloaded, the
browser is supposed to look at the mime type to figure out what to do.
If the mime-type is "image/jpeg", that means it's jpeg image, despite
the fact that it's filename might be "graph.rb" (if it happens to be a
dynamically created image). A mime type of "text/plain" indicates a
simple plaintext file, which should be rendered on the screen. The
browser should NOT try to execute it. If it should be executed, it
should have a mime type of, I don't know, "executable/ruby" or
something. If the browser can't recognize the mime type, *then* it
should feel free to try to figure it out.

A windows pc determines filetypes based on the extension, a mac
primarily uses the creatorcode and typecode metadata, a unix box uses
(mostly, i think) magic numbers. All Things Web are supposed to use
mime types as a bridge. This is to ensure that things like, say, a
plaintext file, don't get interpreted as, say, an executable ruby
script.

um.... okay. I'll stop my "standards rant" now.

-Mark
 
R

Robert Klemme

Mark Hubbart said:
The mime type is there for a reason. When a file is downloaded, the
browser is supposed to look at the mime type to figure out what to do.
If the mime-type is "image/jpeg", that means it's jpeg image, despite
the fact that it's filename might be "graph.rb" (if it happens to be a
dynamically created image).

Correct so far.
A mime type of "text/plain" indicates a
simple plaintext file, which should be rendered on the screen.

I don't subscibe to your relative clause: the fact that data is tagged as
plaintext does not mean it has to be rendered on the screen, especially does
it not mean that the browser should do it. What happens to a file is
totally client (i.e. browser) dependent, so it might or might not decide to
display it itself or delegate handling to some other entity. How this
entity is found (via a browser internal mapping or via some OS mechanism) is
again browser and system dependent.

Pragmatically one would expect a text/plain file to appear on the screen,
probably in a browser window or in some text editor. But it might as well
be saved to disk or whatever, depending on the user's configuration.
The
browser should NOT try to execute it. If it should be executed, it
should have a mime type of, I don't know, "executable/ruby" or
something.

Well, it need not be named "executable", because there is no inherent
semantics in mime type. The configuration determines what to do with it.
You could as well configure emacs as handler for a Ruby script.
If the browser can't recognize the mime type, *then* it
should feel free to try to figure it out.

Agreed, the mime type should definitely take precedence.
A windows pc determines filetypes based on the extension,

AFAIK this is not fully correct: I believe that current Windows version are
also capable to identify a file's type via other mechanisms; I believe you
can write handlers and register them with the system that look into files
and determines type etc. Maybe someone with a bit more Windows insight than
me can comment / correct me.
a mac
primarily uses the creatorcode and typecode metadata, a unix box uses
(mostly, i think) magic numbers. All Things Web are supposed to use
mime types as a bridge. This is to ensure that things like, say, a
plaintext file, don't get interpreted as, say, an executable ruby
script.
Yep.

um.... okay. I'll stop my "standards rant" now.
:)

The crucial point with MIME is, that the standard defines a format for
naming file types and a mechanism to register well known types. But it does
not fix what to do with these files.

Kind regards

robert
 
J

Jon A. Lambert

Kent S. said:
Check out this link:
http://www.moriq.com/ruby/win32-apache2-ruby1.8.1-mod_ruby/

it worked for me in the past.

Thanks for the link Kent. It wouldn't work at all for me. Both ruby-run
and eruby-run crashes the Apache2 child process and causes one to have
to stop and restart Apache. This is under Windows XP.

So I decided to become an Apache module developer (of sorts) and
downloaded the Apace2.0.48 source, compiled it in VC++, then
downloaded mod_ruby 1.1.2 source, hand applied the changes from the
above link, compiled that and ended up crashing and burning just as above.
*sniff*

On a whim, well not exactly a whim as I found some related clues in how
mod_php is compiled and linked, I changed the -MD flag passed to the
compiler to -MT which links in the multi-threaded runtime library staticly
LIBCMT.LIB vs. MSVCRT.DLL, and......

Bingo! I'm now happily writing Ruby and Eruby code type stuffs. Whee!

Thanks for the clue. :)
 
R

Ruby Tuesday

Hi Kents, I tried the site you suggested and still give me the error:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable
to complete your request.

Please contact the server administrator, (e-mail address removed) and inform
them of the time the error occurred, and anything you might have done that
may have caused the error.

More information about this error may be available in the server error log.


----------------------------------------------------------------------------
----

Apache/2.0.48 (Win32) DAV/2 mod_ssl/2.0.47 OpenSSL/0.9.7b mod_fastcgi/2.4.2
PHP/4.3.4 mod_ruby/1.1.1 Server at windev Port 80

And this is the error log from the apache:

[Mon Feb 23 18:55:31 2004] [error] mod_ruby: error in ruby
(eval): (eval)uninitialized constant Apache::RubyRun (NameError)
from (eval):0:in `value'


Below are portion of my httpd.conf for ruby:
LoadModule ruby_module modules/mod_ruby.so

## ClearModuleList
## AddModule mod_ruby.c

<IfModule mod_ruby.c>
RubyRequire apache/ruby-run
<Files *.rbx>
SetHandler ruby-object
RubyHandler Apache::RubyRun.instance
</Files>

# Handle *.rhtml files as eRuby files
RubyRequire apache/eruby-run
<Files *.rhtml>
SetHandler ruby-object
RubyHandler Apache::ERubyRun.instance
</Files>

RubySafeLevel 0
</IfModule>

# CGI for ruby
AddType application/x-httpd-ruby .rb
Action application/x-httpd-ruby /cgi-bin/ruby.exe
 
J

Jon A. Lambert

Osuka Adartse said:
I'm still don't know why Tdiary can use the *.rb files without IE goin'
dialog mmh, btw all the files keep the shebang.

Doing a scan of the TDiary code, it's because it sets the the content-type to text/html
for it's webpages. The only exceptions to setting the content to text/html appear to be
some pages it would produce for severe errors in exception processing (i.e. traceback
output)

The problem is simple but annoying. When Internet Explorer encounters a document
with the content-type of text/plain, it searchs for a helper application based on the
documents extension.

The solution is to change the extension or change the content-type to text/html.

The problem with changing the extension is that if one of the users browsing your
web site has a helper application configured for that extension then IE is going
to use it. For example changing it *.rbx isn't going to help if the user (say me)
decided to associate *.rbx with their favorite text editor (I have). So the only
way to be sure the user sees a document with the content-type of text/plain within
the IE browser is to use no extension at all. That is 'helloworld.rb' becomes
'helloworld'. This solution is annoying for the developer as maybe their favorite
editor's syntax highlighting is keyed of the extension. It's also annoying when trying
to configure your webserver to properly handle files based on their extensions
or having to move these files into special directories or using the location directive.

I've encountered the exact same problems with perl. I understand how convenient
it is to generate the normal EOL sequence in most languages and use text/plain as
opposed to using html-style EOL sequences and use text/html. Unfortunately as
most users are using IE, you are left with these choices.
 
J

Jon A. Lambert

John W. Long said:
---- Jon A. Lambert wrote: ----

I'm running it right now on Apache 2.

Oops sorry, you are correct sir.

I was having trouble under Windows XP. But r.e. my last post, I have that licked.
Hopefully that is, at least I haven't encountered any show stoppers since
yesterday. :)
 
R

Ruby Tuesday

John, would you mind sharing what you did. And if possible, perhaps the
binaries for mod_ruby/eruby and its related static library(if needed). I'd
love to see it working on Apache2/Mod_ruby/Eruby/Ruby-1.8.1

Thanks
 
R

Ruby Tuesday

John, would you mind sharing what you did. And if possible, perhaps the
binaries for mod_ruby/eruby and its related static library(if needed). I'd
love to see it working on Apache2/Mod_ruby/Eruby/Ruby-1.8.1

Thanks
 
J

Jon A. Lambert

----- Original Message -----
From: "Ruby Tuesday" <[email protected]>
Newsgroups: comp.lang.ruby
To: "ruby-talk ML" <[email protected]>
Sent: Monday, February 23, 2004 10:49 PM
Subject: Re: Puzzling... no longer

John, would you mind sharing what you did. And if possible, perhaps the
binaries for mod_ruby/eruby and its related static library(if needed). I'd
love to see it working on Apache2/Mod_ruby/Eruby/Ruby-1.8.1

Thanks

I've zipped up my build directory both binaries and the source and put it here:
ftp://wizards.kicks-ass.net

You have to manually copy the mod_ruby.so to your apache modules directory
and everything in libs/apache to somewhere in your ruby search list.
It was compiled under Windows XP SP1 with VC++ 6.0 using Apache 2.0.48
for ruby-mswin32-1.8.1-11
 
R

Ruby Tuesday

Jon, at last! I've been looking for this day and thank you. It works!

Now, I can start learning Ruby web programming. Thanks again
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,787
Messages
2,569,630
Members
45,338
Latest member
41Pearline46

Latest Threads

Top