automatic instance variable creation during instantiation
=->(){ } 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
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.
Then run Ruby script with rinruby bridge wrapper.
The output looks like:
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]
Here is the crux. You can now wrap these ugly ggplot syntaxes into simple to use ruby method names.
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.
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
# sample ruby code snippet class Posterous def highlight "awesomeness" end end