Posted by Eric Kidd
Sun, 01 Jul 2007 23:00:00 GMT
Programming in Ruby makes me happy. It’s a lovable language, with a
pleasantly quirky syntax and lots of expressive power.
Programming in JavaScript, on the other hand, frustrates me to no end.
JavaScript could be a reasonable language, but it has all sorts of
ugly corner cases, and it forces me to roll everything from scratch.
I’ve been trying to make JavaScript a bit more like Ruby. In particular, I
want to support Ruby-style metaprogramming in JavaScript. This would make it possible to port over many advanced Ruby libraries.
You can
check out the interactive specification, or look at some examples
below. If the specification gives you any errors, please post them in the comment
thread, and let me know what browser you’re running!
Read more...
Tags JavaScript, Macros, Planetary, Ruby | 9 comments
Posted by Eric Kidd
Thu, 01 Feb 2007 23:36:00 GMT
Reginald Braithwaite has just posted a short introduction to closures in Ruby. Closures allow you to pass functions around your program, and build new functions from old ones.
Programming languages that support closures include Perl, Ruby, Python (sorta), Lisp, Haskell, Dylan, Javascript and many others.
The Dylan programming language included four very useful functions built using closures: complement, conjoin, disjoin and compose. The names are a bit obscure, but they can each be written in a few lines of Ruby.
Let’s start with complement:
def complement f
lambda {|*args| not f.call(*args) }
end
We can use this to build the “opposite” of a function:
is_even = lambda {|n| n % 2 == 0 }
is_odd = complement(is_even)
is_odd.call(1)
is_odd.call(2)
compose is another useful function:
def compose f, g
lambda {|*args| f.call(g.call(*args)) }
end
We can use this to pass the output of one function to the input of another:
mult2 = lambda {|n| n*2 }
add1 = lambda {|n| n+1 }
mult2_add1 = compose(add1, mult2)
mult2_add1.call(3)
The conjoin function is a bit more complicated, but still very useful:
def conjoin *predicates
base = lambda {|*args| true }
predicates.inject(base) do |built, pred|
lambda do |*args|
built.call(*args) && pred.call(*args)
end
end
end
We can use it to construct the logical “and” of a list of functions:
is_number = lambda {|n| n.kind_of?(Numeric) }
is_even_number = conjoin(is_number, is_even)
is_even_number.call("a")
is_even_number.call(1)
is_even_number.call(2)
The opposite of conjoin is disjoin:
def disjoin *predicates
base = lambda {|*args| false }
predicates.inject(base) do |built, pred|
lambda do |*args|
built.call(*args) || pred.call(*args)
end
end
end
This allows us to construct the logical “or” of a list of functions:
is_string = lambda {|n| n.kind_of?(String) }
is_string_or_number =
disjoin(is_string, is_number)
is_string_or_number.call("a")
is_string_or_number.call(1)
is_string_or_number.call(:a)
These were four of the first closure-related functions I ever used, and they’re still favorites today.
Feel free to post versions in other languages below!
Tags Hacks, Ruby | 21 comments
Posted by Eric Kidd
Sat, 20 Jan 2007 08:20:00 GMT
New Ruby programmers often ask, “What, exactly, is a symbol? And how does
it differ from a string?” No one answer works for everybody, so–with
apologies to Wallace Stevens–here are 13 ways of looking at a
Ruby symbol.
Read more...
Tags Ruby | 14 comments
Posted by Eric Kidd
Sat, 03 Dec 2005 16:30:00 GMT
Years ago, I looked at Ruby and decided to ignore it. Ruby wasn’t as
popular as Python, and it wasn’t as powerful as LISP. So why should I
bother?
Of course, we could turn those criteria around. What if Ruby were more
popular than LISP, and more powerful than Python? Would that be enough
to make Ruby interesting?
Before answering this question, we should decide what makes LISP so
powerful. Paul Graham has written eloquently about LISP’s virtues. But, for the sake of argument, I’d like to boil them down to two things:
- LISP is a dense functional language.
- LISP has programmatic macros.
As it turns out, Ruby compares well as a functional language, and it fakes
macros better than I’d thought.
Read more...
Tags LISP, Macros, Recommended, Ruby
Posted by Eric Kidd
Wed, 16 Nov 2005 03:13:00 GMT
This weekend, I moved Random Hacks to Typo, a nifty Rails-based blogging system. Here’s what I did:
- Set up my Mac for Rails development
- Pointed Typo at MySQL
- Created a custom theme
- Wrote an article importer
- Routed my old URLs to new locations
- Wrote some custom sidebars
- Configured Debian’s mod_fcgid
Now for the gruesome details.
Read more...
Tags Rails, Ruby, Typo
Posted by Eric
Tue, 11 Oct 2005 04:00:00 GMT
I just recovered the contents of this site after a two-year hiatus.
I'm going to try to dig up some other old stuff, too.
I should really rebuild this whole site using Ruby on Rails and some
Ajax goodness. But that's going to have to wait until I ship some
software at work and take care of other personal projects.
Tags Rails, Ruby | no comments
Posted by Eric
Tue, 11 Oct 2005 04:00:00 GMT
Back in 1961, John McCarthy (the inventor of LISP) described
an interesting mathematical operator called amb. Essentially,
amb hates to be called with no arguments, and can look
into the future to keep that from happening. Here's how it might look
in Ruby.
Read more...
Tags Continuations, Hacks, Recommended, Ruby | 7 comments