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)
You can link to pages with content: animals = ["Cat","Dog","Horse","Fish"] function "animal", [:number] do |num| animals[num.to_i-1] end function "random" do r = rand(4) "%s' % [r, animals[r]] end Visit localhost:8000/random for a random animal link
What about locahost:8000/, though? Easy: function do # stuff goes here, it's a home page end
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 if you want to redirect? There's an intuitive function for that: redirect "source" => "destination" Don't limit yourself, though! redirect "foo" => "bar", "bar" => "other", "third" => "other", "/hi_mom/foo" => "other" (Which would point /foo to /bar, /bar, /hi_mom/foo and /third to /other)
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
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
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/
A simple wiki:
$marshal_wiki = Hash.new
function "create", [:title, :text_70x10] do |title, body|
$marshal_wiki[title] = body
"Page '%s' created." % title
end
function "view", [:title] do |title|
$marshal_wiki[title]
end
function do
'''view
create'''
end
A blog: class Post < Struct.new(:title,:date,:body) def to_html return "
%s
" % [self.title, self.date, self.body] end end $marshal_posts = load_marshal "$marshal_posts", Array path_is "/blog" function "write", [:title_x60, :text_60x10] do |title, text| $marshal_posts.push Post.new(title,Time.now.to_s,text) "Post created." end function "view", [:p] do |p| $marshal_posts[p-1].to_html or "No such post" end function do s = "New Post" 5.times { |i| i += 1 s += $marshal_posts[-i].to_html + "
" unless $marshal_posts.length < i } s end
0.7.4* - Added the _path
variable, and allowed for empty return strings
from page functions
0.7.3 - Removed the original mode of setting the title (returning [body,title]
from a page) and replaced with set_title
and an automatic title set
0.7.2 - Added load_marshal
and fixed a bug regarding the path on file load