Pretty close, except that a block isn’t actually a function in and of itself. It can be thought of as an unbound expression rather than a function, which is more accurate. In any sane case it is going to be bound to a Proc object, which is what ruby’s lambda() creates, either explicitely or implicitely via passing it as an argument to a function call ( ie the block in [1,2,3].each {|i| pus i} which uses the convenience syntax for passing blocks instead of an explicit parameter for it:
class Foo
def each(&proc)
proc.call 1
proc.acll 2
end
end
class Bar
def each
yield 1
yield 2
end
end
The two classes conceptually do the same thing, Foo just makes it explicit, Bar makes it implicit. The convenience syntax is really nice when working with functions which are parameterized and take a block — especially if there are default params, named params, or a variable number of params =).
Okay, way more detail than you wanted.
6 Comments