Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block in Ruby compared to Smalltalk

Tags:

ruby

smalltalk

What does block in Ruby mean? It looks similar with Smalltalk, but you can't send messages to it.

For example, in smalltalk:

[:x | x + 3] value: 3

returns 6. But in ruby:

{|x| x + 3}.call 3

will cause SyntaxError.

Well, you can pass messages to lambda in ruby, though:

irb(main):025:0> ->(x){x+3}.call 3
=> 6

So in Ruby, block is not a block, but lambda is a block? Is this true? I mean, are there any differences between ruby lambda and smalltalk block? If this is true, then what is a ruby block?

Update:

From the comment and answer below, together with some googling, I guess I have more understanding of Ruby block. In Ruby, usually a piece of code evaluates an value, and every value is an object. But, block doesn't evaluate an value. So it's not an object. Instead it can act as part of an object. For example, in {|x| x + 3} can act as a part of the object proc {|x| x + 3 }.

But it did confuse me. In smalltalk, almost every expression can be divided into objects (binding to variables are exceptions). It seems in Ruby, there are more exceptions.

like image 885
weakish Avatar asked Jun 06 '10 10:06

weakish


People also ask

Is Ruby like Smalltalk?

Ruby borrows OOP from Smalltalk, but otherwise is a very much different language. I'm going to argue that Smalltalk is still technically a better choice than Ruby. Ruby appeals to programmers because of its clean, simpler syntax. However, Smalltalk is the ultimate in clean, simple, and minimalist.

What are blocks in Smalltalk?

Code blocks in Smalltalk are self-contained pieces of computation which can be activated by sending them the #value message or one of its variants. A Smalltalk block is Smalltalk's term for a closure. Blocks are instances of class BlockContext.

What is a block in Ruby?

A block is the same thing as a method, but it does not belong to an object. Blocks are called closures in other programming languages. There are some important points about Blocks in Ruby: Block can accept arguments and returns a value. Block does not have their own name.

Why are blocks useful Ruby?

Understanding Ruby Blocks The argument names are defined between two pipe | characters. If you have used each before, then you have used blocks! A Ruby block is useful because it allows you to save a bit of logic (code) & use it later.


1 Answers

First and the most important thing that Ruby block isn't: an object. It is a syntactic construct, and also obviously has an equivalent implementation - but it is not an object, and thus can't receive messages. Which makes your example of

{|x| x + 3}.call 3

ungrammatical. Lambdas, procs - those are objects that wrap a block, and have a call method which executes the block.

Thus, a block is simply a piece of code which can be passed to a method, outside the argument list - no more, no less. If you pass it to Proc.new constructor, for example, it will wrap it and give you an object you can handle:

Proc.new {|x| x + 3}.call 3
like image 135
Amadan Avatar answered Oct 20 '22 19:10

Amadan