Ruby


Facebook development team has open-sourced their light-weight, cross-language development platform, Thrift.

I played around it with it for a while and it looks interesting enough, but I don’t have an immediate need for it.

Check it out if you have a cross-language environment Thrift currently supports C++, Java, Ruby, Python and PHP - a list that should satisfy most everyone.

A POSIX-compliant *NIX system is a requirement, but I’d be curious if it’s possible to get it up n’ running inside of Cygwin (as a development-only exercise, of course).

Here’s another (oh so simple) definition that somehow escaped through the cracks of my brain over time. I’m blushing over this one.

Operators are binary or unary. Binary operators (”bi” as in “two”) have two operands. In “A*B” the * operator has two operands: A and B. In “!B” the “!” operator (meaning boolean NOT) has only one operand, and is therefore a unary operator. The “-” and “+” operators can be both binary and unary, in “-4″ or “+4″ it denotes a negative or a positive number, in “0-4″ it acts as the subtraction operator.

Why’s smooth meta-programming hack is fun to play with and is responsible for exposing gaps in my unary expertise. His code overloads Ruby’s unary ops (-@, +@) creating a clean DSL syntax w/o using any eval hacks. Good stuff.

While studying Lisp documentation on S-expressions and their use of prefix notation, I accidentaly picked up the proper name for the notation I have used all these years (in imperative language land) - infix notation. As always, Wikipedia definition comes in handy…

Infix notation is the common arithmetic and logical formula notation, in which operators are written infix-style between the operands they act on (e.g. 2 + 2). It is not as simple to parse by computer as prefix notation ( e.g. + 2 2 ) or postfix notation ( e.g. 2 2 + ), but many programming languages use it to take advantage of its familiarity.

In infix notation, unlike in prefix or postfix notations, parentheses surrounding groups of operands and operators are necessary to indicate the intended order in which operations are to be performed. In the absence of parentheses, certain precedence rules determine the order of operations.

References to Takahashi-san and Sasada-san are pretty frequent in the Ruby world and I wanted to be clear about how to interpret the -san suffix in the Japanese culture…

San

San (さん?) is the most common honorific title, used when addressing most social outsiders, for example, non-family members. San is used unless the addressee’s status warrants one of the other terms mentioned below.

San is often translated as “Mr.”, “Ms.”, “Mrs.”, and the like. San may also be used in combination with things other than the name of the person being addressed. For example, a bookseller might be addressed as honya-san “Mr. Bookseller”, and a butcher as nikuya-san “Ms. Butcher”.

My two primary languages, Java and Ruby, don’t have explicit support for HOFs, but they both have some related functionality.

Let’s first look at Wikipedia’s definition of Higher Order Functions:

In mathematics and computer science, higher-order functions are functions which do at least one of the following:

  • take one or more functions as an input
  • output a function

The map function found in many functional programming languages is one example of a higher-order function. It takes a function f as an argument, and returns a new function which takes a list and applies the f to each element.

Ruby supports a HOF-like mechanism through the yield keyword. Here’s a snippet from the Bill Venners/Matz interview on Artima:

Bill Venners: Ruby supports blocks and closures. What are blocks and closures, and how are they used?

Yukihiro Matsumoto
: Blocks are basically nameless functions. You may be familiar with the lambda from other languages like Lisp or Python. Basically, you can pass a nameless function to another function, and then that function can invoke the passed-in nameless function. For example, a function could perform iteration by passing one item at a time to the nameless function. This is a common style, called higher order function style, among languages that can handle functions as first class objects. Lisp does it. Python does it .Even C does it with function pointers. Many other languages do this style of programming. In Ruby, the difference is mainly a different kind of syntax for higher order functions. In other languages, you have to specify explicitly that a function can accept another function as an argument. But in Ruby, any method can be called with a block as an implicit argument. Inside the method, you can call the block using the yield keyword with a value.

Apache has an effort under way to bring partial HOF support to the Java world, the Commons Functor project. The project looks pretty interesting, but might end up being pretty verbose to use. If its usability is similar to that of the CollectionUtils/Predicate functionality in the Apache Commons, I’m willing to give it a try. Either way, I hope Commons Functor comes of the Sandbox soon and starts playing with the big boys.

Here’s a tool that I didn’t know I couldn’t do without - Andrew Birkett version of Smalltalk’s MethodFinder. Fabulous stuff.

I needed to record the source code for later…

class Object
  # Clone fails on numbers, but they're immutable anyway
  def megaClone
    begin self.clone; rescue; self; end
  end
end

class MethodFinder

  # Find all methods on [anObject] which,
  # when called with [args] return [expectedResult]
  def self.find(anObject, expectedResult, *args )
    anObject.methods.select { |name| anObject.method(name).arity == args.size }.
      select { |name| begin anObject.megaClone.method( name ).call(*args) ==
        expectedResult;  rescue; end }
  end

  # Pretty-prints the results of the previous method
  def self.show( anObject, expectedResult, *args )
    find( anObject, expectedResult, *args ).each { |name|
      print "#{anObject.inspect}.#{name}"
      print "(" + args.map { |o| o.inspect }.join(", ") + ")" unless args.empty?
      puts " == #{expectedResult.inspect}"
    }
  end
end

UPDATE: Syntax in why’s version of MethodFinder is even more intuitive. Going along with his recommendation I also need to add this to my ~/.irbrc.

UPDATE #2: RedHanded users added a bunch of tweaks that ended up in this .irbrc file

TryRuby

Why? Ask Why.

Use the pretty print package! (thanks DT)

irb(main):001:0> require ‘pp’
=> true
irb(main):002:0> a = [’abc’, ‘def’, ‘ghi’]
=> [”abc”, “def”, “ghi”]
irb(main):003:0> pp a
[”abc”, “def”, “ghi”]
=> nil

37signals launched Backpack earlier this week
to much fanfare in the blogosphere.

5 free pages for the free account does not particularly strike my fancy,
but I do have to give them props for launching another sweet looking
(and apparently very successful) Ruby on Rails/Ajax site.