Skip to content

Instantly share code, notes, and snippets.

View tarcieri's full-sized avatar

Tony Arcieri tarcieri

View GitHub Profile
>> o = Object.new
=> #<Object:0x1110cdc>
>> class << o
>> FOO = 'bar'
>> end
=> "bar"
>> class << o; self; end::FOO
=> "bar"
>> foo = 42
=> 42
>> foo
=> 42
>> eval "foo", TOPLEVEL_BINDING
NameError: undefined local variable or method `foo' for main:Object
from (irb):3:in `eval'
>> self
=> main
>> puts(fun do
.. puts("Why hello there")
.. puts("How are you doing?")
.. 42
.. .to_s())
..
#<Lambda>
=> nil
>> [x.to_s() | x in 1..10]
=> ["1","2","3","4","5","6","7","8","9","10"]
# Find Pythagorean triples
>> n = 50
=> 50
>> [(a,b,c) | a in 1..n, b in 1..n, c in 1..n, a+b+c <= n, a**2 + b**2 == c**2]
=> [(3,4,5),(4,3,5),(5,12,13),(6,8,10),(8,6,10),(8,15,17),(9,12,15),(12,5,13),(12,9,15),(12,16,20),(15,8,17),(16,12,20)]
# Yeah, this doesn't look very impressive, except keep in mind it's
# compiling to Erlang forms, and Erlang has single assignment semantics
# This code now goes through Reia's Static Single Assignment transform,
# which lets you rebind variables by mapping them to versions.
#
# Example (from shell)
# >> Foo.bar(42)
# => 85
module Foo
% Input:
[{module,1,
{constant,1,'Foo'},
[{function,2,
{identifier,2,bar},
[{identifier,2,n}],
[{match,3,
{identifier,3,n},
{op,{'*',3},{identifier,3,n},{integer,3,2}}},
{match,4,
# Another Reia rebinding example, this time using pattern matching
module Foo
def bar(a, b)
(a, b) = (a + 1, b + 2)
b = b * 2
a + b
>> class Foo
.. def get_bar
.. @bar
.. def set_bar(val)
.. @bar = val
..
=> ~ok
>> f = Foo.start()
=> #<Object>
>> f.get_bar()
require 'rubygems'
require 'rev'
class MyHttpClient < Rev::HttpClient
def on_connect
super
STDERR.puts "Connected to #{remote_host}:#{remote_port}"
end
def on_connect_failed
/*
* Copyright (C) 2007-08 Tony Arcieri
* You may redistribute this under the terms of the MIT license.
* See LICENSE for details
*/
#include <assert.h>
#include <string.h>
#include <time.h>