December 2010
36 posts
Merry Christmas everybody! Full disclosure: this is my particular gift to Open Source, and future programmers everywhere.
Hackety Hack is a project that I inherited from _why. It’s the best way for people who’ve never learned programming to get their feet wet. It uses Ruby, combined with a GUI toolkit named Shoes, another _why project, to make it really easy to make all kinds of animations, games, and other applications.
Here’s an example bit of code:
label, time = nil, Time.now Shoes.app :height => 150, :width => 250 do background "rgb(240, 250, 208)" stack :margin => 10 do start = button "Start" do time = Time.now label.replace "Stop watch started at #{time}" end stop = button "Stop" do label.replace "Stopped, #{Time.now - time} seconds elapsed." end label = para "Press start to begin timing." end endHere’s a screenshot of this in action:
As you can see, it uses Ruby’s blocks extensively.
Shoes isn’t just for really simple apps. Hackety Hack is itself written entirely in Shoes. :) Here’s an example of Pong in Shoes:
http://www.youtube.com/watch?v=b37OibG0BBk
The source code for this actually comes with Hackety, as an example. It’s about 50 lines
There’s also a web application that it integrates with. You can upload the programs that you make and then share them with other people.
Anyway, enough chatter. Enjoy your holiday! Merry Christmas!
[Github] [Website GitHub] [README] [Website] [Development Mailing list]
Yes, you heard me right. Yesterday, the Snap framework had its 0.3 release. If you haven’t heard of Haskell before, you’re in for a treat.
I’ll try to keep this short. Haskell is a functional programming language. This makes it a bit different than the procedural or object oriented languages you’re probably used to. The syntax looks much more like math notation than code:
f :: Int -> Int f x = x + 1This reads “f is a function that takes an Int and returns an Int, and f of x is equal to x + 1.” For more about Haskell, check out Haskell.org. I could talk forever about how interesting and different Haskell is.
Enough about that. Let’s get back to Snap. To install Snap, use
cabal, the Haskell package manager:$ cabal install snapNext, make a directory and create a project:
$ mkdir hello-snap $ cd hello-snap $ snap initThis gives you a directory with a
.cabalfile and a source directory. Then try this:$ cabal install $ hello-snap -p 8000 $ curl 'http://localhost:8000/'; echoThis’ll compile your website (yes, you read that correctly, Haskell is compiled), start a server on port 8000, and then request the homepage.
That’s all fine and dandy, but what’s the code look like? Well, if you look at the handler in
Site.hs, you’ll find this:site :: Application () site = route [ ("/", index) , ("/echo/:stuff", echo) ] <|> fileServe "resources/static"While the syntax may be foreign, you can get the gist of it: We route ‘/’ to index, and anything starting with ‘/echo/’ to an echo handler. Otherwise, serve a file from the
resources/staticdirectory.What about one of these handlers? Here’s the
echohandler:echo :: Application () echo = do message <- decodedParam "stuff" heistLocal (bindString "message" message) $ render "echo" where decodedParam p = fromMaybe "" <$> getParam pDon’t be scared! It’s easy. Repeat after me:
echois anApplication. When it’s called, we first decode the “stuff” parameter and bind it tomessage. We then use a Heist template and bind the “message” string inside to ourmessagewe created from the parameter, passing that along and using it to render the “echo” template. Oh, and thatdecodedParammethod that we used to set up themessageearlier gets the parameter we asked for, (p) and yields either it or an empty string. You can find a much more thorough explanation of this in the documentation, which is excellent.So what’s new in v0.3? There are three big improvements, as far as I’m concerned:
- SSL support
- “Development Mode”, which means you don’t need to recompile your site every time you change your code. This is a big benefit to getting things done quickly.
- an extensions mechanism for writing reusable components.
If you’d like to see an entire website written with Snap, the Snap website is itself written in Snap, which is also on GitHub.