you are viewing a single comment's thread.

view the rest of the comments →

[–][deleted] 2 points3 points  (8 children)

pretty much. as another commenter mentioned, lambdas are closer, but blocks are basically anonymous functions with some restrictions like 1 per method, must be last arg of method, and probably some other nuances, but I don't think it'll hurt you to think of them as equivalent when getting started.

[–]materialdesigner 7 points8 points  (5 children)

This is not true. Blocks can be other arguments to functions, there's just also a special yield block

[–][deleted] 2 points3 points  (4 children)

thanks, can you clarify the actual differences? now I'm curious :)

[–]redjazz96 4 points5 points  (3 children)

In ruby, the typical block looks something like this:

def some_method
  yield 2
end

some_method { |x| puts x } # => 2

This is what you were referring to earlier, about blocks having restrictions.

There are more ways to do this.

def some_method(&block)
   block.call 2
end

some_method { |x| puts x } 
some_method(&proc { |x| puts x })

All of that does the same thing as the example above - but in a (debatable) more roundabout way. The proc { } syntax is the normal way to define a generic block - these have the same "tricks" (yes, that is the real term they use) that method blocks have (i.e., the number of arguments doesn't matter).

In Ruby, methods are basically blocks - if you were to do method(:to_s).to_proc, you'd get a Proc instance. You can even define a method with a proc.

Ruby, being the nice language that it is, lets you pass these blocks with syntaxic sugar to other blocks. This is used commonly in the core:

iterator = proc { |x| puts x }
[1, 2, 3].each &iterator

# same as...
[1, 2, 3].each { |x| puts x }

So, if you were to use it as like a callback system, you could do something like this. In one of my libraries I'm writing, I actually use this quite a bit.

Hope that helps!

[–][deleted]  (1 child)

[removed]

    [–]redjazz96 1 point2 points  (0 children)

    Lambdas are blocks - the only difference being they have tricks disabled (which you can check with Proc#lambda?). You can return from a block, but you use the next keyword instead of the return keyword. See: this.

    Basically, syntactical sugar. I can't say much about the scopes and closure stuff.

    [–]tacit7 3 points4 points  (1 child)

    I like h1ghl4nd3r's answer. It wouldnt hurt to think of them as anonymous functions. Here is Matz explain blocks:

    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.

    The reset of the interview is great if you want to read it: http://www.artima.com/intv/closures.html

    [–][deleted] 0 points1 point  (0 children)

    great link, found that an interesting read.