0

I am trying to use Ruby to set up a home page. I currently have this code:

#!/usr/bin/ruby -w

puts "Hello, Ruby!";

It is saved as testing.rb. I am able to host files onto my school's public server so I had it in my school's directory such as school.ca/myname/public_html/testing.rb.

This has worked for previous HTML, CSS, Perl, ASP, and PHP programs. Now I am trying to set up Ruby and I'm having trouble. All it displays on the URL is my source code from above.

I changed the file permissions to 644.

4
  • 2
    This is relevant - Ruby isn't like PHP in that you can just drop it in and everything magically "works". Commented Nov 13, 2015 at 18:20
  • @Makoto, thanks for the link, really helpful! Commented Nov 13, 2015 at 18:28
  • All it displays on the URL is my source code from above. Try giving your ruby file a .cgi extension. Commented Nov 13, 2015 at 19:58
  • It looks like you're trying to use Ruby as a CGI. I'd highly recommend reading about Sinatra, which is Ruby based, and is modern web-serving technology. It's much lighter-weight than Rails and the learning curve is subsequently much easier. And, it's very capable of handling complex tasks. Commented Nov 13, 2015 at 19:58

3 Answers 3

1

You need a Ruby aware web server. There is one solution that makes running Ruby almost as easy as running PHP: Phusion Passenger. It can be installed as an Apache or Nginx plugins, two very common web servers. However, in order to install these plugins you usually require root access to your server, which I assume you don't have.

An entirely different solution would be to use a cloud service provider such as Heroku. They offer free plans as long as you are ok with your app not running 24h/day. In order to use this service, you have to be familiar with git though, but then it is as easy as uploading your code through git to run your application.

As for building your first Ruby web application, you should check out Sinatra. A simple hello world application would look as follows:

require "sinatra"

get "/" do
  "Hello World!"
end

Sinatra is a Ruby Gem. You can install those libraries from the command line using a tool called gem:

gem install sinatra
Sign up to request clarification or add additional context in comments.

3 Comments

I think I'll give a shot using the Sinatra method. However, Im a bit lost, where do I put the "require sinatra" code?
A Sinatra application can live in a single file. So you just put it at the top of your file. All five lines of the example above go into the same file.
@GreatShark, require 'sinatra' is not going to work unless you install the sinatra gem on your school's server, which I'm guessing they are not going to allow you to do.
1

I changed the file permissions to 644.

~/ruby_programs$ chmod 644 cgi.cgi
~/ruby_programs$ ls -al cgi.cgi
-rw-r--r--  1 7stud  staff  102 Nov 13 15:50 cgi.cgi

File permissions are displayed as follows:

  1. First character is - or d: - means file, d means directory

  2. Then there are three sets of three characters indicating permissions for owner, group and other:

r = readable
w = writable
x = executable

644 produces the permissions:

 rw-r--r-- 

which are equivalent to:

owner: rw-  (read, write)
group: r--  (read only)
other: r--  (read only)

You don't have to know who is an owner, or who is part of a group, or who falls in the other category to recognize that no one has permission to execute the file. You need to do this:

~/ruby_programs$ chmod a+x cgi.cgi   #=>all + x => give execute permissions to everyone
~/ruby_programs$ ls -al cgi.cgi
-rwxr-xr-x  1 7stud  staff  102 Nov 13 15:50 cgi.cgi

Now the permissions are:

 owner: rwx
 group: r-x
 other: r-x

which means that now anyone can execute the file.

All it displays on the URL is my source code from above.

If you haven't already done so, try giving your ruby file a .cgi extension. Then use this code:

#!/usr/bin/env ruby

puts "Content-type: text/html\n\n"
puts "<html><body>Hello, Ruby!</body></html>"

Are you sure ruby is installed on your school's server?

Comments

0

Ruby is not a language that browsers can really interpret on their own without help - you need to either set up a server and have it print out an HTML webpage yourself, or use a framework that does so for you(i.e. Sinatra, Rails).

2 Comments

Browsers don't interpret PHP either.
This is true - those languages are pre-processed by the servers into a format the browser understands. Ruby isn't without some setup.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.