23
Sep 08

Ruby’s Kernel#rand for random numbers within an interval

As part of some Ruby-Processing work I’m doing, I needed a random number within a specified range. Processing has its own random function, but I wanted to use Ruby.

I needed a random number in the interval (-range, range). I immediately thought, “specify the max value range*2 as a param to rand, and then subtract range to get it into the interval -range to range.” My autopilot first attempt yielded:

    num = rand(range*2.0) - range

However, I didn’t read the documentation for Kernel#rand closely. According to the docs, the max parameter is converted to an integer:

    max1 = max.to_i.abs

More…

22
Sep 08

Thomas Keller’s Midnight Snack

I keep forgetting where this is. It’s Thomas Keller of the French Laundry, Bouchon, and Per Se. The voice-over inflates the sandwich (world’s greatest?), but it does look mighty tasty.

19
Sep 08

Body Navigation – Play Pong, get Fit!

I’m a big fan of Processing, and who can complain about a project involving dancers, a choreographer, and a programmer? I love the pong part. I gotta defrost my projector sometime and see what I can do with it.


Body Navigation by Recoil Performance Group from ole kristensen on Vimeo.

13
Sep 08

That’s not XTC! iTunes Store Feature Suggestion

I use the iTunes Store as a way to find and preview music, and I’ve purchased quite a bit of music and TV shows through it. I’ve seen several cases where music has been mis-labeled or mis-categorized. For example, the last two albums on this page for the UK band XTC:

I’m not the only one who noticed this particular problem.
More…

08
Sep 08

Batch Compiling with Aquamacs Emacs

I needed to batch compile some files recently, and I had to lookup how to do this with Aquamacs. It’s pretty simple. If Aquamacs is at /Applications/Aquamacs, then this will work:

/Applications/Aquamacs\ Emacs.app/Contents/MacOS/Aquamacs\ Emacs -nw -batch -f batch-byte-compile /path/to/elisp-file.el

05
Sep 08

Confusion Over Triple Equals (===)

Doesn’t everyone have concepts that they can’t keep organized in their heads? I frequently get values for certain keys mixed up in my head, especially ones that I don’t use very often. At the Lone Star Ruby ConferenceGreg and I were talking about the triple equals operator in Ruby recently (===), and I confused my notion of it in Ruby with what I thought was its function in Perl. Obviously I don’t use it very much. Hopefully I can clear this up though.

In Perl, there is no triple equals operator, based on the perlop Perl operator documentation. I was really mixing things up there.

In PHP, the triple equals operator is used to test whether two things have the same value and type, according to the PHP Manual.

<?php
    $a = "1";
    $b = 1;

    if ($a == $b) {
      print "$a == $b";
    }

    if ($a === $b) {
      print "$a === $b";
    }

?>

This code prints

1 == 1

In Ruby, triple equals (Object#===) is, “effectively the same as calling #==, but typically overridden by descendants to provide meaningful semantics in case statements,” based on the Object class documentation. So, for classes like Array, #=== is effectively the same as #==. I say effectively since I haven’t actually perused the source of array.c, though you can see in object.c that rb_equal (===) calls == and then checks id_eq if that doesn’t return true. And in the case of your own objects, triple equals can be used to provide your own equality tests for case statements.

04
Sep 08

Emacs Lisp – Block Commenting Ruby Code

These days I’m working to become proficient in Emacs (I’ve recently switched from Vim, as Greg mentioned on his blog). With that comes all kinds of things like org-mode, yasnippet, and lots of keyboard combinations.

A part of this current effort I’m learning Emacs Lisp right now. There’s probably a better way to do block comment cycling in Ruby, but this was good practice on writing some simple elisp functions.

(defun ruby-comment-region ()
  "In Ruby, comment out the current region with =begin/=end statements."
  (interactive)
  (save-excursion
    (goto-char (point))
    (insert "=end")
    (goto-char (mark))
    (insert "=begin\n")))

(defun ruby-uncomment-region ()
  "In Ruby, uncomment the current region -- remove =begin/=end statements."
  (interactive)
  (save-excursion
    (if (re-search-backward "^=begin")
        (replace-match ""))
    (if (re-search-forward "^=end")
        (replace-match ""))))
03
Sep 08

Bomomo for a bit of fun

If you’re in need of some doodling, try out Bomomo. I like the mass transit map feel that some of the drawing tools have.

And you can save your drawings too.


Copyright © 2010 ANDY PAYNE
Powered by WordPress and Theme Lab