Tag Archives: fun

James Edward Gray: Associative Arrays and Ruby Hashes

Yesterday I put out a little screencast showing some ways of Creating Ruby Hashes. James Edward Gray II pinged me on Twitter and basically said “Great screencast! Ooh, but you forgot this! Ooh, and this! And this!” and so of course there was nothing to do for it but invite him to do a pairing screencast with me.

This video is a bit of a weird hybrid. You get 7 minutes of podcall, then 18 minutes of screencast, then another 12 minutes of podcall. James shows off some of the “hot new awesomeness” of Ruby 1.9, and then points out that this awesomeness has been around for a couple of years and nobody’s using it, in spite of it having been in the current Pickaxe for nearly as long. Along the way we talk about regular expressions, testing dogma, and the importance of never squashing creativity in the open source community. All in all, an incredibly fun time for me. James threatened to come back and do another one with me on regular expressions, and I’m mentioning it here in writing so that everybody knows I plan on taking him up on that offer.

No podcast, because half of it is us typing into a shared screen session. But here’s the video. You may need to watch it on Vimeo or download it to see the font clearly.

Associative Arrays with James Edward Gray II from David Brady on Vimeo.

Twitterable Mandelbrot II: The Mandelbrottening

Yesterday I posted my Twitterable Mandelbrot, a ruby script that generates the Mandelbrot Set in 134 characters. A few of you took this as a challenge to shorten my code even further. I didn’t mind, and in fact was interested to see your results; I was sure that an extra character here or there could be shaved off.

What I didn’t expect at all was that somebody would shave fourteen characters off.

Reader brahbur on rubyflow came up with this:

a couple of these changes could be considered “cheating” (-:

80.times{|a|p (0..300).map{|b|x=y=i=0;(x,y,i=x*x-y*y+b/150.0-1.5,2*x*y+a/40.0-1,i+1)until(x*x+y*y>4||i>98);i>98?0:1}*''}

Brahbur’s solution does look different; there are quote marks on each line and it outputs 1s and 0s instead of #s and .s, but the mandelbrot is still clearly visible (Edit: I reduced the size from 300×80 to 240×60 just to keep the outputs roughly the same size):


Click for larger version (1400×800)

I think this is just awesome. Once we’re playing with 0’s and 1’s, I can see another optimization: i>98?0:1 can be replaced with 99i. This bring us down to 118:

60.times{|a|p (0..240).map{|b|x=y=i=0;(x,y,i=x*x-y*y+b/120.0-1.5,2*x*y+a/30.0-1,i+1)until(x*x+y*y>4||i>98);99i}*''}

I have to give most of the credit to brahbur, though–I just saw a tiny tweak, on top of the amazing rewrite they already did. So great. THANK YOU brahbur!

Now, the challenge continues: can you shorten this further? Brahbur was concerned about “cheating”, so let’s define the rules for clarity: Output should be 240×60 (extra quotes and padding are okay) and it should be visually recognizable as a Mandelbrot set. Other than that, go for it.

Twitterable Mandelbrot

As a kid I always thought fractals were neat, but every time I tried to learn how to do them, I got lost in the math. I guess 20 years makes all the difference: today I went and read up on the Mandelbrot set and had one of those “wait, that’s it?” moments.

It took me about 15 minutes to write the program. Here’s the output:


Click for larger version (1400×800)

The whole program was about 400 characters long. I got to thinking, “that’s *almost* small enough to fit into a single tweet…” and then I spent the next hour and a half refactoring my code for size.

Victory:


http://twitter.com/dbrady/status/12546255974

What do you mean I need a hobby? I have one. See?