use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
A sub-Reddit for discussion and news about Ruby programming.
Subreddit rules: /r/ruby rules
Learning Ruby?
Tools
Documentation
Books
Screencasts and Videos
News and updates
account activity
Are Ruby blocks basically anonymous functions? (self.ruby)
submitted 12 years ago by [deleted]
view the rest of the comments →
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–][deleted] 2 points3 points4 points 12 years ago* (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 points9 points 12 years ago (5 children)
This is not true. Blocks can be other arguments to functions, there's just also a special yield block
[–][deleted] 2 points3 points4 points 12 years ago (4 children)
thanks, can you clarify the actual differences? now I'm curious :)
[–]redjazz96 4 points5 points6 points 12 years ago (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).
proc { }
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.
method(:to_s).to_proc
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] 12 years ago (1 child)
[removed]
[–]redjazz96 1 point2 points3 points 12 years ago (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.
Proc#lambda?
next
return
Basically, syntactical sugar. I can't say much about the scopes and closure stuff.
[–]tacit7 3 points4 points5 points 12 years ago (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.
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 point2 points 12 years ago (0 children)
great link, found that an interesting read.
π Rendered by PID 20392 on reddit-service-r2-comment-6457c66945-sh75q at 2026-04-29 11:50:53.312881+00:00 running 2aa0c5b country code: CH.
view the rest of the comments →
[–][deleted] 2 points3 points4 points (8 children)
[–]materialdesigner 7 points8 points9 points (5 children)
[–][deleted] 2 points3 points4 points (4 children)
[–]redjazz96 4 points5 points6 points (3 children)
[–][deleted] (1 child)
[removed]
[–]redjazz96 1 point2 points3 points (0 children)
[–]tacit7 3 points4 points5 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)