99 bottles of (ruby-colored) beer on the wall
I wanted to create a really simple web page with Ruby On Rails.
I have been using HTML for about a dozen years. I have been doing some Ruby programming off and on for the past 6 months or so. I wanted to put the two together in an RHTML page.
Here is what I generated:
4 bottles of beer on the wall,
4 bottles of beer.
Take one down, pass it around -
3 bottles of beer on the wall.
3 bottles of beer on the wall,
3 bottles of beer.
Take one down, pass it around -
2 bottles of beer on the wall.
2 bottles of beer on the wall,
2 bottles of beer.
Take one down, pass it around -
1 bottles of beer on the wall.
1 bottle of beer on the wall,
1 bottle of beer.
Take one down, pass it around -
no more bottles of beer on the wall.
Turned out to be pretty easy to coax my server to display this output. Other than zombie-drone work typing in the HTML and the non-varying parts of the text, all I did was read the parameter and execute a loop - using Ruby.
Here is the HTML that makes that web page show up:
<html>
<head>
<title>Beer Song in Ruby On Rails</title>
</head>
<body>
<pre>
4 bottles of beer on the wall,
4 bottles of beer.
Take one down, pass it around -
3 bottles of beer on the wall.
3 bottles of beer on the wall,
3 bottles of beer.
Take one down, pass it around -
2 bottles of beer on the wall.
2 bottles of beer on the wall,
2 bottles of beer.
Take one down, pass it around -
1 bottles of beer on the wall.
1 bottle of beer on the wall,
1 bottle of beer.
Take one down, pass it around -
no more bottles of beer on the wall.
</pre>
</body>
</html>
That is the goal actually, not the solution to the problem.
The whole solution is just about fifteen lines of RHTML, most of which is plain, old HTML.
The interesting part that generates the portion of the page that actually executes multiple times - and handles changing the text each pass through the loop - is here:
<% start = params[:start].to_i %><% start = 99 unless start!=0 %>
<pre>
<% start.downto(2) do |bottles| %>
<%= bottles %> bottles of beer on the wall,
<%= bottles %> bottles of beer.
Take one down, pass it around -
<%= bottles-1 %> bottles of beer on the wall.
<% end %>
I made a couple mistakes as I put this together.
- Tried using the pound-sign/braces formatting trick that works in all Ruby double quoted literals.
- Tried using a more terse expression to set the default value of the start variable.
As you can see, neither of these things required a complicated fix.
Kicking it off was a piece of cake. The URL looks like this:
http://www.yourhost.com/beer_song/sing?start=4
The need to have a start parameter instead of just hardcoding the value 99 as the loop count is dubious, I admit.
I forced that requirement in so I would have to read a parameter from the request and use its value in my view. Plus, I wanted to be able to have sample output that would not require 20 pages of output!
As I worked myself through these little self-generated exercises, I of course generated a couple application errors along the way. That was quite useful, actually.
I was impressed by the quality of the error messages. To me, an error message has to tell the person reading it what they are supposed to do - if anything - to fix the problem (or help get it fixed). Rails error message was great. I am the developer. It gave me a nice stack trace.
My next task is to create a few tables in an SQL database. Then I can have a page that modifies and displays these values in a web page.
I have a couple of useful applications in mind. So after I get these newbie etudes out of the way, I can move on to them.
At that point I will be checking off a lot of things on my personal TODO lists.
0 Comments:
Post a Comment
<< Home