Peculiar similarities between Ruby and Smalltalk
Ruby takes a lot of inspiration from Smalltalk. Some of the similarity is odd, though.
Smalltalk symbols and method syntax
In Smalltalk, symbols use the #
sigil (e.g. #foo
). In Ruby, symbols use the :
sigil (e.g. :foo
).
In Smalltalk, you can get a method for a given name (selector) using lookupSelector:
, e.g. someArray lookupSelector: #sort
. The name (or selector) of the method is a symbol. Ruby refers to methods using that same syntax as well, e.g. #first
— at least in documentation, because #
is used for comments in Ruby, not symbols.
The syntax for symbols using the :
sigil comes from Lisp.
Each/do and blocks
In Ruby, you use #each
to iterate over a collection. A block is created using do
…end
:
things.each do |thing|
…
end
In Smalltalk, you use do:
to iterate over a collection. A block is created with [
…]
, and the conventional name for the argument is each
:
things do: [:each | …]
Block argument variables start with :
, which resemble Ruby’s symbols.
When local variables are needed, they can be added using double pipes, e.g.
things do:
[:each |
|myLocal|
…]
The double pipe syntax resembles Ruby’s syntax for block parameters.