ThisBindle::Code::Ruby

I'm currently learning Ruby/RoR. Below is some initial dabbling, mostly focusing on what can be done with first-class functions.

Unlambda Interpreter

Status: In progress

Description:
A interpreter for the obfuscated functional programming language, Unlambda. Currently I've implemented a turing-complete subset of the language, including write capability. The interpreter now ignores all non-printed whitespace and comments as it should.

Source: ruby-unlambda_0.2.tar.gz
Includes:

To do:

Quines

A quine is a program that prints out it's own source code.

This is a quine I wrote with some nice symmetry. It's almost a line-level palindrome.

a="a=%p\nb=%p\nc=%p\nputs a%%[a,b,c]\nputs c"
b="c=%p\nb=%p\na=%p"
c="puts b%[c,b,a]"
puts a%[a,b,c]
puts c
puts b%[c,b,a]
c="puts b%[c,b,a]"
b="c=%p\nb=%p\na=%p"
a="a=%p\nb=%p\nc=%p\nputs a%%[a,b,c]\nputs c"

The above is based heavily on a ruby quine currently tied for the shortest known:

_="_=%p;puts _%%_";puts _%_

Highlight the box below for an explanation of what this quine is doing (I wouldn't want to spoil it I guess...)

First it assigns '_' to the string "_=%p;puts _%%_".
Then it calls puts on '_%_', where '%' acts as an operator that applies the string '_' as a formatting string to itself.
As a formatting string, '_' says to print '_=' followed by the inspection of the string it is formatting, followed by ';puts _%_'.
The inspection of '_' is just itself surrounded by quotes, and we're all set.

Probabilistic Quines

I thought I'd look into making some ruby programs that were quines, just perhaps in a way that's astronomically unlikely.
For example, this program that seems like a quine with probabilty (1/94)38

_="";38.times{_<<(rand(94)+32)};puts _

or this one that would be the shortest ruby quine (some of the time):

25.times{putc rand(126)}

It turns out that it's highly unlikely that any of these are capable of ever being quines due to the limits of random number generators.
That is, since there are only 2^32 seeds possible, it's very likely that none of the seeds will produce the one combination out of 126^25 that I need for the later would-be quine. If I could just generate arbitrary data (like the quines above) with some appropriate seed, I'd effectively have compressed that data to a single int.

Lazy Evaluation

Source:
lazylist.rb.gz

Description:
A very mild exploration into lazy evaluation in Ruby.
The LazyList class takes a first element, a proc to apply to an element to get the next one, and an optional proc test to apply to those elements to put them in the list. The list then supports the standard array notation for accessing one element or a slice with a specified legth, e.g.

myList[0]
myList[2, 200]

For example, you could get a lazy-evaluated list of the natural numbers with

nat = LazyList.new(1, Proc.new {|n| n + 1})

or numbers divisble by 7

nat = LazyList.new(1, Proc.new {|n| n + 1}, Proc.new {|n| n % 7 == 0})

Not too thrilling, but the list isn't limited to numbers of course, so something interesting could be done like

desired_foos = LazyList.new(foo, Proc.new {|o|, o.step}, Proc.new {|o|, o.desired?})

I guess now I just have to figure out something that would need this. I really just wanted to try out lazy evaluation, since Ruby's block/proc features lend themselves to it.