Comment by vbit

8 years ago

This applies to loops as well. Numbers are objects that accept the `timesRepeat:` message:

    10 timesRepeat: [ n := n*2 ].

Not only that, you can send messages to blocks, to implement loops.

    [ n < 10 ] whileTrue: [ n := n + 1 ]

Compare to Ruby:

    10.times { n = n * 2 }

The latter isn't possible "out of the box", but easy enough to implement:

    class Proc
        def whileTrue; yield while self.call; end
    end

    # And use:
    n = 1
    ->{ n < 10 }.whileTrue { n = n + 1 } 

    puts n

Ruby syntax does get in the way of some of the more succint ways of defining control structures in Smalltalk, but the above shows some of the Smalltalk heritage of Ruby quite well, I think (though, while "10.times" is fairly idiomatic Ruby, implementing constructs like the one below certainly is not).