Entries RSS Comments RSS

Archive for the ‘javascript’ Category

Ruby, Javascript, and HTML/CSS (and Sinatra)

Wednesday, March 24th, 2010

I’ve mentioned before that I’m a bit of a Sinatra (http://sinatrarb.com) fan. Recently, at the Interlock hackerspace, I’ve been learning javascript from a friend/mentor. We’ve talked about the best ways to teach programming, and decided a fun way to start would be with javascript games. I really enjoy javascript so far. We started off using a text editor (gedit) and browser (firefox) to develop a simple tic tac toe game. This was a good way to start. As soon as I went home for the night, I (switched to vim, initialized a git repo), moved the app over to Sinatra, and deployed with Heroku. My restructuring took about 5 minutes, deployment took 5 seconds. Here is what I did.

Restructuring the Application

Initially, I had a directory which included a .js, .html, and .css file. To restructure my app for Sinatra, I changed my ~/projects/tictactoe directory to look like this:

|– config.ru
|– public
| |– css
| | `– style.css
| `– js
| |– tictactoe.js
|– tictactoe.rb
`– views
`– layout.erb

Breaking down Sinatra

config.ru – I mentioned earlier that I deployed with Heroku (more on that in another post, but basically Heroku is a very simple, git-driven way to deploy rack applications– and you can use it for free). The config.ru file tells Heroku that this is a rack application. The contents of the file? Simple. “Sinatra::Application” That’s all I need.
public – Your javascript and your CSS live here. Have as many as you want, and you simply link to your javascript and your css is your .erb files in your views directory, just as you would in the index.html of a site. Nothing fancy there.
views – choose the templating language of your choice, and use it here. I use erb because I’m most familiar with it, but haml is exceedingly popular as well. Anything in layout.erb will show up on *every* page. Since my tic-tac-toe application is only one page, I only need one layout. I’ll talk about what happens if you have more pages in the next section.**
tictactoe.rb – this is where the magic happens. Right now, this is all this file contains:

require 'rubygems'
require 'sinatra'

get '/' do
erb :layout
end

require tells my app the dependencies it needs and loads them for me. get ‘/tiktaktoh’ gets that route for me. do initiates the block that I’m about to pass which contains erb :layout, meaning my layout.erb file will be my ‘view’ for this route. And, end closes my block. I have routed my application to the index page of my url. Check it.

Why the heck did I do *THAT*?

First, I can easily expand this. I can make jsgames.heroku.com and have ‘/tiktaktoh’ route to my tiktaktoh layout, which would reference my stylesheet and my javascript for tiktaktoh. I can also make ‘/bingo’ do the same thing. I can easily create a separate bingo.js and bingo.erb file to create that project completely separate. This is perfect for learning and keeping my code organized. All of this is easily handled through Sinatra’s routing capabilities in one easy file — maybe I’d call it games.rb instead of tiktaktoh.rb, and it might look like this:

require 'rubygems'
require 'sinatra'

get '/tiktaktoh' do
erb :tiktaktoh
end

get '/bingo' do
erb :bingo
end

jsgames.heroku.com/tiktaktoh would go to my tic-tac-toe app, and so on.

**I mentioned earlier that I will still have a layout.erb that will be my default layout, but I will only keep things I want on *all pages* there (like header tags) and then I will insert the ruby (erb) code <%=yield%> in layout.erb wherever I want the layout specific to each game to show up (bingo.erb or tiktaktoh.erb). If my bingo.erb and tiktaktoh.erb layouts are drastically different, I might limit my layout.erb file to header tags. Then, I would simply insert <%=yield%> between my opening and closing html tags. My bingo.erb and tiktaktoh.erb would then include all of the formatting necessary to make my games look pretty.

And second, after I set this up, I deployed with heroku in a quick and easy step, but that’s for another blog post.

Conclusion

I now have a well-organized, expandable, git-supervised and deployable project directory. And although it might seem like overkill for starters, I love how easy it is to develop locally and then deploy with a single command for free, and we can start with ajax requests whenever we want, when we get there. :)

You can follow my slow and steady progress at http://github.com/ashumz/tiktaktoh My thoughts are to turn this experience into a nice tutorial on beginning javascript for (new) rubyists.

Currently Computing:
Me: HELLO AWESOME! What’s your name?
Awesome: Bowline, a ruby/js GUI framework

Judging books by covers: Learning a programming language

Saturday, January 16th, 2010

Today I ditched a work meeting to go grocery shopping, and then ditched the grocery shopping idea to go to Barnes & Noble and browse the programming section.

So far, I have read the Apress Beginning Ruby book and some of The Pragmatic Programmer book on Ruby.

I spent two hours looking at different approaches to teaching different levels of different languages. Lately I have taken an interest in software developed for the purpose of teaching basic concepts and implementations of computer programming. It was incredibly interesting to see the vast number of approaches to conveying the basics and also the intricacies of languages like Javascript, PHP, Ruby, and Python. I even found some colorful illustrated books attempting to teach C and C++! From those particular titles, though, I came to the hasty conclusion that colored illustrations and comic sans’y fonts don’t necessarily make it easier to grasp concepts. In fact, sometimes such a flowery approach can even distract from the concepts being taught. For me (and arguably most new learners), the delivery of the author and the clarity of examples and structure make or break the author’s approach. (I did buy one of the said ‘flowery’ books and will review once I am finished.)

For a compromised solution of visuals and thorough structure/delivery, I found Peachpit Press has a line of books called “Visual Quickstart Guide”’s. I can deal with the slightly presumptious title of the series, only because such a line of books exists that claims you can learn a language in 10-24 hours (depending on the thickness of the book and the complexity of the subject, I guess.) Compared to that claim, visual quickstart didn’t irk me too much. In the Visual Quickstart Guide series, there is a strong visual component, though I’m unsure as to how well these visualizations aid the teaching of the subject matter. I did buy one of these books as well– on Javascript and Ajax. I will review my experience with this book at some point, too.

The Head Start O’Reilly books looked really interesting in their approach, as well. These books utilize comic sansy fonts and waste a lot of space with illustrations, but cover a lot of ground from beginner to intermediate throughout the course of the text. Throughout the books, readers are prompted to solve problems (on lines meant for text! write your code in pencil!) and to think critically about a given problem or prospective solution. I was deciding between their book on Programming (actually an intro to Python that also covers basic programming concepts) and Hello World! Computer Programming for Kids and Other Beginners (which also uses Python the same way.) In the end, the latter was roughly $20 cheaper, so it won. I was also attracted to the fact that it was geared towards kids, but not explicity limited to kids by welcoming beginners as its readers as well. The Head Start books do not explicitly state that they are for kids or beginners in their title, but it is clearly implied when reading the book description and flipping through the illustrated pages and goofy fonts. In fact, the two books covered basically the same items (and utilized similar tools for their tutorials even.) After I am finished with Hello World! perhaps I will invest in a Head Start book for another subject. I’d be interested to know if anyone has read any of those books to learn a language, and how it went.

On an entirely unrelated note, first Linux workshop at Interlock was a success! I need to go home now though. I will regail my blog with my readings and childish Python adventures soon.