timrandg's Space

ruby processing grids

rvm install jruby

rvm use jruby

sudo gem install ruby-processing

 

Screenshot_01

Lambda syntax

=->(){ } is equivalent syntax to lambda{| |    }

There is not real advantage over creating a function here. We could just as well use:

def max_of(y, z)

    y > z ? y : z

end

 

 

Automating Graphing from Lego Mindstorm Sensor Data File

Collect data while looping and writing from a sensor to a data file. Here Elliot and I are collecting distance data from the ultrasonic sensor. 

Screenshot_01

Then run Ruby script with rinruby bridge wrapper.

The output looks like:

Screenshot_02

 

Adding actual data value to ggplot2 barplot

The code above adds the values to the ggplot2 barplot.

Bargraph_with_numbers

graphing, meet scripting

This is the holy grail--R plotting from ruby code. This is just to remind myself of some of the tricks. 

Note that arrays are vectors in R land [1,2,3] ==> c(1,2,3).

You can prove this with 

r.y = [1,2,3]

p r.pull "dput(y)"  # => [1,2,3]

Screenshot_05

Screenshot_06

Here is the crux. You can now wrap these ugly ggplot syntaxes into simple to use ruby method names.

The Little Schemer-inspired ruby

I wondered what it would take to write a Array#flatten-like method. Immediately, it reminded me of the scheme list manipulating exercises in The Little Schemer. But the question was could I remember how to pull off the recursion. 

The rules of recursive functions are:

1. You must check for an escape condition.

2. You must make the recursive call on a chunk of the original thing that is closer to the escape condition than the starting point. 

The code below is what I ended up with. Straight forward ruby. If it were scheme, the array[0] and array[1..-1] would be replaced with (car array) and (cdr array).

The meat and potatoes line is the one with collection + self[0].rec_flatten + self[1..-1].rec_flatten. This line can never be called on an empty array--because an empty array would force a return before that point. It can be called with a simple array like [2]. In that case the car of [2] is 2, but the cdr of [2], should be []. If we ask ruby for the [2][1] item, will it give nil or []? Let's check irb:

>> [2][1..-1]  # => []

>> [2][10] # => nil

So it looks like it gives what we need, namely the empty array--though it seems like nil would be the most sensible answer. If we index far past the end of an array, we get nil. If we called nil.rec_flatten, we would get [nil]. But luckily array[1..-1] behaves just like (cdr array), giving an empty array back.

syntax highlighting seems to be broken for code snippets

Screenshot_01

Hi posterous, I tried to use markdown with a code snippet as described in your documentation. The code is in a block, but appears black and white, not colored. The screen shot is attached. Please let me know if this feature will be restored. I need a simple way to post code from markdown. Thanks, Tim

Testing Markdown

Heading

# sample ruby code snippet
  class Posterous
    def highlight
    "awesomeness"
    end
  end