Embed URL

HTTPS clone URL

SSH clone URL

Basics

The method function will create a new page:
function "home" do
"Hello, world!"
end
Save it as any Ruby file (*.rb), then run scratch.rb and point a browser to
http://localhost:8000/home to see the result

Arguments can be added (with a given prompt):
function "home", [:name] do |thename|
# Note the variables don't have to be the same
"Hello, %s!" % thename
end

You can specify the size of the input (xNumber is a text box, NxN is a textarea)
function "home", [:name, :school_x20, :desc_30x10] do |name, school, desc|
"Hello, %s! You go to %s, right? %s" % [name, school, desc]
end

Note that the last string returned will be the resulting display. \n is
automatically converted to <br />, so don't worry about manually doing
that.
function "count", [:to] do |to|
show = String.new # AKA ""
to.to_i.times { |i|
show += i.to_s+"\n"
}
show
end

Anything that starts with 'marshal_' (ex, marshal_data) will be run through
Marshal and saved to its name.db (ex, data.db) for later loading:
marshal_data = Array.new
function "item", [:name, :desc_x40] do |name, desc|
marshal_data.push([name,desc])
end
That will leave a "data.db" containing the array of items you added (by visiting
localhost:8000/item

As for loading those same variables? There's an easy way to load marshal_ data,
though the load_marshal function:
marshal_data = load_marshal "marshal_data", Array
It will either return the loaded data, or a new object of the data type you
specify (in thise case, an array)

What about locahost:8000/, though? Easy:
function do
# stuff goes here, it's a home page
end

Asthetic Stuff

Some non-core but possibly useful tools also included in scratch.rb are paths
and manual page titles.

Paths let you make virtual folders. For example:
path_is "/test"
function "hello" do
"Hello, world!"
end
That will create, instead of localhost:8000/hello, localhost:8000/test/hello

This lets you create pages with the same name in different places:
function "found" do
"YOU LOOK LOST"
end
path_is "/secret"
function "found" do
"YOU FOUND THE SECRET!"
end
Visiting localhost:8000/secret/found will run the "found" method, versus just
localhost:8000/found ("YOU LOOK LOST")

What about redirecting in the middle of a page function? There's a redirect_to
for that:
function "test" do
if rand(2) == 1 then
redirect_to "/home"
else
"You got a 0 from rand(2)!"
end
end
Which would randomly send viewers of /test to /home or show them a string.

Normally, the title is just the function name, minus any paths that may be tacked
on to the URL. But you can make your own, if you want:
function "home" do
set_title "Blog"
"Hello, welcome home."
end
Instead of having the page title of "home - Mozilla Firefox" (or whatever your
default title is), it will be known as "Blog - Mozilla Firefox". Since it's a
string, you can mess with it, of course, including dynamic titles;
function "home", [:user] do |user|
set_title "%s's Home" % user
"%s isn't here right now." % user
end

Some other stuff

Let's talk special variables. These are added to pages' variable request arrays,
and start with an underscore (:_varname):

_path => The current path (ex, /blog/view [minus ?var=val])

_req => The WEBrick::HTTPRequest

_res => The WEBrick::HTTPResponse

Conclusion

You can create entire, simple web apps from scratch.rb. It will load every Ruby
file it finds in the current directory, including all of their required files.
You could set up an app like:
app/
|-- scratch.rb
|-- app.rb
|-- data/
`-- scripts/
|-- more_stuff.rb
And it would run just fine, providing app.rb loaded the files inside of scripts/