Entries RSS Comments RSS

Archive for the ‘Sinatra’ 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

Easy Sinatra Project

Saturday, January 30th, 2010

I lovelovelove Sinatra. (SinatraRB.com) I created a very simple app (the fortune cookie generator I mentioned in an earlier post) to play with styling/views, get and post requests, and the very bare bones workings of Sinatra. I absolutely love how pure and simple it is. The structuring is a bit confusing at first, but the Sinatra Peepcode video helped me a lot on this: http://peepcode.com/products/sinatra

In the Peepcode video, they make a Sinatra app in one file, and then break it down from there into separate files. It’s good to see how everything CAN fit in one file. This is how I learned CSS, actually– by putting my styles in with the rest of my website. Once I realized you put the CSS in a separate file, I had a mini epiphany: sometimes it’s best to put things together before you take them apart. Then you can truly appreciate the fragmentation of a given application and truly understand the purpose of breaking things up for simplicity’s sake. Seeing this helped me understand a lot about Sinatra, and even shed some light for me on why Rails is the way it is, as well.

So, dundundun: I made this: http://fortunefinder.heroku.com

Don’t judge my poor styling and wacky coloration. ;) This really was just a silly little experiment to play with the basic features of Sinatra. Now that I understand the basics, I’m moving on to another project confidently. I’ll still probably tweak this one a little, too. Just for fun.

You can check out the code on github at http://github.com/ashumz/fortunefinder Nothing special, but might help a Sinatra newbie get a feel for the basic set up of a simple app.


Sinatra: The answer to my Ruby GUI problem

Monday, November 9th, 2009

Ruby and GUI’s have been on the forefront of my brain lately because I’ve been playing around with fun ways to program with Ruby. I am honestly bored of command line programs, and for good reason: the command line isn’t pretty enough for me. Since I’ve had problems installing Shoes and Hackety Hack on my version of Ubuntu, I asked one of my friends who uses Ruby frequently if there were other GUI options. There are options, but he referred to them as a bit of a mess to implement and pointed out that javascript and the web make for better uses of, what I would call, “fun Ruby.” Better yet, when you program for the web, everyone can use your program without worrying about what platform they are on.

So, I gave this a little thought. I started getting jealous and even a little frustrated at Python programmers and their connectedness with GUI’s and their pride in Python’s support for GUI’s. Apparently, Ruby has similar support, but it’s not nearly as widely embraced amongst Rubyists as it is in the Python community. (True? False? opinions?) Admittedly, I think I should have just learned visual basic and got my GUI fix 10 years ago. (I don’t particularly want to learn Python, and there are issues with it’s syntax that have turned me off pretty heavily.)

So, I have decided that the best course of action, given my new fixation on Sinatra, is to begin a jquery/javascript and Ruby project. Although I’ve been making websites for about 10 years, they have generally been informative, usually static, very low level applications of HTML, CSS, and bits and pieces of PHP. I have used javascript for fun little menus now and again, but honestly I’m a total copy/paster when it comes to js. I started recently on some javascript tutorials, but was then quickly reprimanded: SKIP TO JQUERY, says everyone. Unless you really wanna become a pro in javascript, it sounds like jquery is a nice shortcut to the overwhelming syntax (especially coming from Ruby!) and implementations of javascript. I can’t quite figure out a consistency of why and how something does what in javascript. But, if I can figure out what I want the code to do, I’m pretty sure I can figure out how to use jquery to get the job done. We’ll see.

I am going to begin working on a blog that is purely in javascript, using jquery, and also uses Sinatra. I’m going to use sqlite for the DB. Did I mention I have very little experience with DB’s, too? :) Okay, this project, honestly, seems a little over my head to me right now. But once complete, I think I will have experience with all of these pieces I need to make a pretty, fun, and useful application. Something every programmer craves– right?! :)

Updates to follow. Thanks to Julio for helping me come up with the idea on how to practice messing with all of the components I’m craving: Ruby, Sinatra, Javascript/Jquery, Sqlite, ERB, CSS… wee! This brings up another point about how multilingual and implementation exposure at the early stages of learning a programming language can be so incredibly integral to understanding how to best utilize a programming language like Ruby… but that’s for another post! Cheers!

Ruby on Rails or Sinatra?

Saturday, November 7th, 2009

My updates have been a little scarce, but I’ve been programming– I promise. :P

I recently have been working on a site, discountcampusbooks.com (psst, buy your books there!) with my husband, alan.dipert.org. The book search is written in Ruby and uses the incredibly cute Ruby framework, Sinatra. I absolutely LOVE Sinatra, and I think for me it is a viable alternative to Rails. I’ve had pretty mixed feelings on ROR lately. I know I am a bit too opinionated for my skill level, but I really have something to say about the differences and advantages of both Rails and Sinatra.

I should say this first, because I feel like it:

For someone like me, who is just learning their first programming language, new to object orientation, and incredibly naive about the power or lack thereof of a given language, I feel blessed to have picked Ruby as my first language to learn. As I have learned Ruby, I’ve also learned the shortcomings of other languages. I am lucky enough to have a multilingual husband who keeps books like toilet paper in our bathroom, so at any given point I can pick up a little reading material and learn a little Clojure (yeah!!) or C (not so much). I’ve explored Python and Haskell pretty thoroughly as well as a few other common programming languages. I can’t argue against any of these languages too much. Why would I? Mostly, as I learn more about other languages, I just feel luckier to have picked Ruby as my devirginizing language. :P I also know it is important to explore the value of other languages. In fact, I know I sometimes need other languages to make the best use of Ruby. There is so much to say about all of this. End interjection (for now.)

The biggest issue for me, as I mentioned earlier, is the fact that ROR is the largest use/implementation of Ruby and the easiest for which you can find tutorials, screencasts, support groups, chatrooms, etc etc. Overwhelmed by ROR, I started using a “joke” (literally) framework my friend wrote to get my Ruby programs off the command line and on the web. After dealing with the shortcomings of his babyframework (again, it was written as a “joke” and was actually put together as a way to use Ruby as if it were PHP), I started branching out on my own. It became clear to me that, though you can use Ruby in a similar way to PHP using ERB, it’s not necessarily the best way. I had been working on a dungeon text adventure in Ruby and I was trying to figure out how to make it work via the web using ERB. I was, for the most part, successful. My tech support was limited, but I started to see the parallels of his framework and Sinatra after checking out Sinatra earlier in the week. Enter Sinatra. Things like Rack ran on both the joke framework and Sinatra. I could even use shotgun to run a webserver on my computer. WHOA. All of these things were exposed to me in small doses, and then adding Sinatra to the mix just blew my mind further. Ruby is fuckin’ cool. Noted. And I continued to compute.

When my husband asked me to help with the book search, I was pretty excited. I was so excited to see an actual implementation of a Sinatra-based web app. There was so much Ruby involved!! :) During my first experience with rails, I was hardly able to figure out where I should even put my Ruby. :) Weeks earlier, I had spent time creating a ’sample blog’ in rails, and figuring out the MVC framework. In rails, so much of the work is done for you. A large part of the process is accepting MVC framework, also seen in zend and django in PHP and python, respectively. You don’t need to be a Ruby expert to do Rails. (But you can be, and you will be better for your knowledge, that’s for sure. I have never doubted that.) Likewise, I truly believe you could suck at Rails and excel at Ruby. In fact, you’d probably prefer and have already used Sinatra if this is the case. They are not mutually inclusive. One of my favorite things about Ruby is the power and extensiveness of the language. Frameworks as light as Sinatra give creative freedom and your exposure to Ruby and the web can be vast or minimal– you can most likely get something to work for you. If you want something big, you’ll have to build it yourself. On the other hand, larger frameworks provide you an out of the box, large-scale solution that subsequently requires more knowledge and familiarity of the framework before you can customize it creatively.

Sinatra allows you a very simple, lightweight framework that requires you build off your own knowledge rather than, as my friend Julio would put it, David’s skills. Honestly, if you know the scope of your site will be huge, you know your DB infrastructure, and you’ve been through all of this before, Rails is probably your answer. From what I hear, you’ll love it, and it’s worth learning the MVC framework aspects that may be, at first, completely fuckin’ confusing (for me, it’s slowly coming along after using Sinatra more). However, if you’re like me, and you wanna start from small potatoes and build it yourself (yeah!!), use Sinatra. I feel like I may be making my way towards Rails, but I may also never get there. I enjoy the freedom of Sinatra, and I’m not building any gigantic web applications for the time being. Sinatra just rules if you love Ruby, but Rails seems to big and Ruby command line programs aren’t GUI enough. The web is an excellent agnostic platform for development. So embrace it. I have noticed many programmers are reluctant to do so for various reasons, but Sinatra has opened my eyes to the fact that you can write painless, creative Ruby apps with a little hassle and a lot of fun.

Cheers :)