Metaprogramming Ruby

Saturday, 19 April 2008, w kategoriach: Programming, Ruby

On this year’s RuPy I gave a talk about Metaprogramming Ruby. Unfortunately I didn’t have the time to prepare the presentation exactly the way I wanted, nor did the organisators gave me enough time to present what I wanted to present. Anyway, I made some changes to the slides, which are now of some value on they own I believe, so I put them here for everybody to enjoy. Things covered include: Ruby’s object model, differencies between instance_eval/class_eval, hooks, reflection, wrapping methods, DSLs, Higher-Order Messanging, getting the parse tree from Ruby code and vice versa, and so on. There is also a nice resources section, where you will find the gems I found across all the available material about the topics covered, and from which I’ve picked up a lot of really interesting meta-ideas. If you find any errors, please let me know, this stuff is really fuzzy, and I really want to have it covered in a clear and precise way.


Despite the quirk with my own presentation, I really enjoyed RuPy as a participiant and I think it’s a great event. Zed Shaw had a really nice presentation, with some funny bits, but also with many things you can pick up and use weeks after the conference ended. I also liked the talk about PyPy by Carl Friedrich Bolz, even through I’m a Ruby guy - they really have some interesting ideas about compiling/interpreting dynamic languages and they made a hell of the effort to push it forward. I hope I will be able to visit Poznań again the next year and I encourage you to do the same too.

10 programming languages, 10 projects, 12 months - The List

Wednesday, 2 January 2008, w kategoriach: Programming

Here is what I finally settled on for my little project:

Languages:

  • C# (Mono)
  • Common Lisp (SBCL)
  • Python
  • Factor
  • Scala
  • Erlang
  • Haskell
  • OCaml
  • Smalltalk (Squeak)
  • Prolog (SWI)

I replaced Scheme with Scala, as Ashley Moran suggested in the comments to the previous post. Also, I removed J from the list, as I was afraid about not finding enough libraries or support to finish my projects in any reasonable time. I added Prolog instead, because of it’s long tradition, and some very positive opinions by excellent programmers (Joe Armstrong in particular).

Projects:

  • Ray-tracer
  • Unit testing framework
  • HTTP server (serving a single directory)
  • Blog web application (with a web framework if available)
  • Arkanoid-style game
  • Generating an index from a set of text files
  • Hex editor
  • Basic bindings to the Allegro library
  • A mini-language compiled to CSS (simplifying the handling of large style sheets)
  • A GUI front-end to “cdrecord”

Even through I won’t turn any of those into a full-fledged, production-ready application, I still highly doubt if I will manage to do all this in one year, especially considering the fact that I don’t have huge amounts of free time. However, I won’t stress about this too much - if it takes more than 12 months, or if I won’t complete it for any reason, so be it. It just sounds like a lot of fun, so let’s not be too serious about this.

10 programming languages, 10 projects, 12 months

Wednesday, 2 January 2008, w kategoriach: Programming

Hello, I’m Jarek and I’m a programming languages junkie. From since I was a young nerd, I enjoyed playing with them, and to some degree I still do. As I get older and more serious about programming, through, I try not to get as subjective about them as I once was, or worse yet, attached to a particular technology too much. Sadly, I found such attitude to be rare, and good objective language evaluations hard to find, especially ones that would cover which disciplines which language is good for, except the obvious ones (like Perl for text processing). A good craftsman knows what tool to use for which tasks, this is pretty much established knowledge, even through some variation of opinions certainly exists. Yet in programming, other factors tend to play a major role in choosing the tool - large corporations support (if the project is a commercial one, or if you want to be useful in your work), personal taste, hype surrounding the language, ease of use etc.

Erlang, the functional Smalltalk?

Saturday, 1 December 2007, w kategoriach: Programming, Erlang

Let’s start with a confession. I’m not a Erlang weenie, I’m not even a decent part-time Erlang hacker. But for some reason, I keep coming back to it, even through I don’t normally work in the problem domain I think Erlang is best suited for, like servers of various kinds (from MMORPG games to Jabber), or large scale, distributed applications. But in this case, it works differently - I keep coming up with problems that could be nicely solved in Erlang, instead of matching the language to the problem.

I wrote about the great fun factor of programming in Erlang before, but I didn’t quite get the reason this is the case. Now, after thinking about for a while, I think I know what Erlang does so well, that it makes programming a joy.

Html generation in JavaScript

Sunday, 10 June 2007, w kategoriach: Programming, JavaScript

I started playing a little bit more with JavaScript recently, and one thing that bothered me was generating HTML. For example, if you have a AJAX application of any reasonable size, it probably has to change a lot of stuff inside of the various divs or other elements as it runs. For that, you need to write a lot of ugly-looking, long-running string concatenations. So, I took an idea used by the Lisp folks or in the Ruby Markaby library and I created a library to write HTML in JavaScript. I’m not sure if I’m the first one to come up with this, but my Google-fu failed in this case, so maybe… Anyway, with my JsTemple you write something like this:

$table({cellpadding: 10},
  $tr(
    $td(
      $div({klass: ‘container’, id: ‘main’},
        $h1(‘Foobar’),
        $h2(‘Foobar2′),
        $h3(‘Foobar3′),
        $h4(‘Foobar4′),
        $h5(‘Foobar5′),
        $div({id: ‘layer’, style: ‘border: solid 3px #f00; padding: 10px;’},
          $p({klass: ‘foo’},
            ‘foo’, $strong(‘bar’)),
          $p({klass: ’shmoo’},
            ’shmoo’))))));

And you get this:

<table cellpadding=“30″>
  <tr>
    <td>
      <div class=“container” id=“main”>
        <h1>Foobar</h1>
        <h2>Foobar2</h2>
        <h3>Foobar3</h3>
        <h4>Foobar4</h4>
        <h5>Foobar5</h5>
        <div id=“layer” style=“border: 3px solid #f00; padding: 10px;”>
          <p class=“foo”>foo<strong>bar</strong></p>
          <p class=“shmoo”>shmoo</p>
        </div>
      </div>
    </td>
  </tr>
</table>

I generated the necessary data about tags / possible properties from HTML 4.01 W3C specification, so all the tags should work and invalid properties should be ignored. “Should”, because the generating was a little bit tricky and I’m not 100% sure if everything is the way it should be. The dollar sign is a bit annoying, too, but my goal was to avoid cluttering of the global namespace, especially a variable name like “i” may be quite popular… I don’t know how it works performance-wise or memory-wise either (certainly some optimizations are possible), but it seems to work under Firefox, Opera, Konqueror (Safari?) and MSIE 6/7 and it even doesn’t crash any of them in a short time, what I consider a big success. I don’t really know if this is useful at all (most people don’t seem to like balancing parenthesis or don’t know a decent editor which highlight pairs of matching parenthesis, judging by Lisp popularity), but you can grab it here. To start using it simply include the js file using <script>, nothing more. If you somehow find it fun, just leave a comment. Afterwards, feel free to do whatever you want with the code.

One more tip: keep in mind that jsTemple just returns a string, so you can for example use it with Prototype’s Template class.

Erlang For The Practical Man

Wednesday, 9 May 2007, w kategoriach: Programming, Erlang

Table of contents:

1: General overview
2: Making an RSS
3: Fighting with POP3 / SSL
4: Serving the thing through HTTP
5: Putting the pieces together
6: Conlusions

This article is my submission for the Erlang blogging contest. You can also grab it as a PDF file - in a4 format or in letter format.

1: General overview

Functional programming languages are often underestimated, as they seem to be not suited for the “real world”, being thought of as designed by scientists who spend their time mainly developing theories, not practical software. What makes Erlang stand out from the crowd is the fact it was designed by the industry and for the industry. Ericsson, where the language originated, has a telecom switch with more than 2 million lines of Erlang code running it that has a downtime of a few minutes per year - now this is reliability!

Telecom switches don’t make programmers too excited those days through, so we will try to do something more related to your everyday hacking. In this tutorial I will show you how to write a standalone daemon, that checks your email every x minutes, and makes the results available as an RSS feed. If you want to centralize all your notifications around RSS this may even be useful a bit and even if not, at least it makes a good example of some of Erlang strengths. I try to explain everything in as much detail as possible, but be warned - we won’t be writing any “hello worlds” here, we will just rapidly introduce a lot of awkward stuff as it becomes necessary, so if you don’t have a solid programming background you may have serious trouble. Either way, you will probably want to consult Programming Erlang and the free, official Erlang docs if you want to do anything serious in Erlang. Also see my previous post for an overview of general high level Erlang concepts.

Having Fun With Erlang

Monday, 12 March 2007, w kategoriach: Programming, Erlang

It seems that the times when programs run sequentially using one main processor are finally getting to an end… Not only more and more computations get so complex you need to use a whole network of computers to do them, not only computer clusters are increasingly popular, but also our own home computers finally have become somewhat “parallel”. As we reached the border where we can’t tune the processor frequencies so much, chip vendors seem to look for hope in multi-CPU, multi-core circuits as in Intel Core 2 Duo for example. This has multiple benefits, but also raises one big issue - the troubles with writing programs that use the multiple chips or even multiple computers efficiently. At the same time, the Web still grows, and we have more and more systems where hundreds of thousands of people interact with the software in real time (examples vary from MMORPGs to web servers).

Language of the gods? C.

Sunday, 18 February 2007, w kategoriach: Programming

When you look at popular articles/posts about programming in the last few years, one topic comes back over and over and each time its extremely popular - the “enlightenment” that comes with learning Common Lisp, Scheme, Haskell or sometimes yet another not-so-mainstream language like Smalltalk. Recently people started even bragging about the “language of the gods”. I think the thing that started the buzz was the “Howto become a hacker” text. But I didn’t seen any particular too much wonderful Lisp applications written by ESR…

After thinking about it for a while, come on, it’s total bullshit. Someone can conclude from those articles that learning macros, continuations, monads or whatever will turn one into a alpha programmer, or a super-human. Yes, those are all interesting concepts, they stretch your mind, you get a better programmer as you learn it. I greatly appreciate it and I try to learn things like that myself all the time. But don’t get trapped into believing that this is THAT important - there are other as important or even more important fields no-one is blogging about. Its also a lot like saying that just getting a new guitar will make you a better guitarist. The language is just a tool, maybe it’s time to stress more other topics like problem solving techniques, good design skills, which are current all the time.

The road to become an alpha programmer

Sunday, 10 December 2006, w kategoriach: Programming

Update: Version 1.2, 07.05.07 - A lot of typos and grammar errors fixed, added new positions again, rewritten some of the descriptions.

Update: Version 1.1, 21.03.07 - I reorganized the whole thing and rewritten various parts of it, added some new positions, expanded the mind-map etc.

So, you wanna be a programmer, but you don’t have a fortune to spend to start learning in a decent college nor whole days for studying. Or maybe you’re too young to go to a college, but you wanna get an idea of what CS is about. Or you hate people because you’re a nerd, or you got a sucking score on the SATs, or you didn’t make it to the MIT like I didn’t, or whatever…

The good news is: I’m 100% sure one can get really, really good on your own at home - it needs a lot of dedication and effort, through. It’s much easier to just sit whole days and read posts on weblogs about how to get good at something than to really start studying hard (speaking from my own experience). I found that I already have so much stuff that I want to learn that I don’t need even more new inputs to overwhelm me, so I cut out a lot of programming related blogs from my feed reader. The article below is supposed to be a road-map primarily for myself, so feel free to make suggestions, but remember it isn’t meant to be objective, authoritative or complete.

Emacs packages roundup

Tuesday, 31 October 2006, w kategoriach: Programming, Getting Things Done, Emacs

A lot of knowledgeable people swear by Emacs. But when you first open it up you can get scared. Its hard to open a file without entering a mysterious key combination, there is no syntax-highlight, no file browser, no auto-indent… So what’s the reason? There are actually three of them I believe:

  1. Emacs is a editor-building framework, not an editor itself. If you want to use it, you need to customize it. Using a plain Emacs doesn’t make much sense.
  2. A lot of the nice stuff is in the CVS version. Eg. a lot of people is using stable versions of Emacs with some ugly X11 toolkit on Linux, where actually there is a much better version with GTK2, Xft and lots of other nice stuff, but you need to grab it from CVS.
  3. You need to get to know the various packages Emacs provides to explore it full power