Jump to content

"\r\n" are for some reason turned into "\r\n\r\n"; what's wrong?

- - - - -

  • Please log in to reply
1 reply to this topic

#1
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
I have this Ruby program:
#!/Ruby/bin/ruby.exe 


print "HTTP/1.0 200 OK\r\nContent-Type: text/plain\r\n\r\n" 


class Greeter 

	def initialize name='default' 

		@name= name.capitalize 

	end 

	def salut 

		print "Hello #{@name}! \r\n\r\n" 

	end 

end 


print "Hello. \r\n\r\n" 


g= Greeter.new "world" 

g.salut 


g= Greeter.new 

g.salut

But for some reason ruby outputs "\r\n\r\n" when I tell it to output "\r\n"; anyone know how to fix that? Maybe something like binmode (as in Perl)?

Here's what it's supposed to output (hexadecimal):
0x485454502F312E3020323030204F4B0D0A436F6E74656E742D547970653A20746578742F706C61696E0D0A0D0A48656C6C6F2E200D0A0D0A48656C6C6F20576F726C6421200D0A0D0A48656C6C6F2044656661756C7421200D0A0D0A
And here's what it outputs (hexadecimal):
0x485454502F312E3020323030204F4B0D0A0D0A436F6E74656E742D547970653A20746578742F706C61696E0D0A0D0A0D0A0D0A48656C6C6F2E200D0A0D0A0D0A0D0A48656C6C6F20576F726C6421200D0A0D0A0D0A0D0A48656C6C6F2044656661756C7421200D0A0D0A0D0A0D0A

(I converted the stuff to hexadecimal before pasting it here.)

#2
RhetoricalRuvim

RhetoricalRuvim

    JavaScript Programmer

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,254 posts
  • Location:C:\Countries\US
Okay, I figured it out:
#!/Ruby/bin/ruby.exe 


STDIN.binmode 

STDOUT.binmode 


# the rest of the code ... 

I have two files, hello.rb and hello.html (the .html file has the same Ruby code as the .rb file; my server doesn't mind if the extension is different). For some reason when I go to /hello.html it opens the page normally, but when I go to /hello.rb it asks me whether I want to open or save the file (it doesn't, however, do that, if I change "text/plain" to "text/html" (I'm talking about the "content-type" HTTP header)).




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users