0

I've just started on ruby and can't wrap my head around blocks

How is it different from an anonymous function?

On what instance would I want to use it?

And when would I choose it over an anonymous function?

wnoveno
  • 546
  • 3
  • 9
  • 25
  • 1
    I don't think we can create an anonymous function without blocks. "Ruby supports anonymous functions by using a syntactical structure called block." – mask8 Sep 11 '12 at 23:32

1 Answers1

4

Ruby doesn't have anonymous functions like JavaScript (for example) has. Blocks have 3 basic uses:

  1. Creating Procs
  2. Creating lambdas
  3. With functions

An example of where blocks are similar to anonymous functions is here (Ruby and JavaScript).

Ruby:

[1,2,3,4,5].each do |e| #do starts the block
  puts e
end #end ends it

JS (jQuery):

$.each([1,2,3,4,5], function(e) { //Anonymous *function* starts here
  console.log(e);
}); //Ends here

The power of Ruby blocks (and anonymous functions) is the fact that they can be passed to any method (including those you define). So if I want my own each method, here's how it could be done:

class Array
  def my_each
    i = 0
    while(i<self.length)
      yield self[i]
      i+=1
    end
  end
end

For example, when you declare a method like this:

def foo(&block)
end

block is a Proc object representing the block passed. So Proc.new, could look like this:

def Proc.new(&block)
  block
end

Blocks, by necessity, are bound to a method. They can only be turned into an object by a method like I described above. Although I'm not sure of the exact implementation of lambda (it does extra arg checking), but it is the same idea.

So the fundamental idea of a block is this: A block of code, bound to a method, that can either be contained in a Proc object by an & argument, or called by the yield keyword.

Linuxios
  • 34,849
  • 13
  • 91
  • 116
  • so a block is just a block of code that can be passed to method? What's the difference in using the lambda keyword then? Or is my problem just the vocabulary of the language? – wnoveno Sep 12 '12 at 00:24
  • @wnoveno, this blog post gives a pretty decent explanation: http://www.robertsosinski.com/2008/12/21/understanding-ruby-blocks-procs-and-lambdas/ – Mischa Sep 12 '12 at 00:29
  • @Linuxios, you forgot `i += 1` in your `my_each` ;-) A for loop may be better here... – Mischa Sep 12 '12 at 00:30
  • 1
    @Mischa: Ruby doesn't have `for` loops in the C sense. And the `for` loops it does have are really just calls to `each`. – Linuxios Sep 12 '12 at 00:31
  • `Proc` careted by `lambda` is more like javascript's anonymous function as you can posibbly receive the return value from the proc in a same way you can do in js. `Proc.new` is a slightly different as its return means really return. It's more like just a block if you want to call it. anyway blocks are used for those, and that's the purpose of it – mask8 Sep 12 '12 at 00:36
  • If you want to know the difference b/w `lambda` and `Proc.new`, this may help http://stackoverflow.com/questions/626/when-to-use-lambda-when-to-use-proc-new – mask8 Sep 12 '12 at 00:39
  • @Linuxios, I always use `.each do`, so I never realized a classic for loop doesn't exist in Ruby. What a shock! – Mischa Sep 12 '12 at 00:51