Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ruby's <code>yield</code> is not an iterator like in C# and Python. <code>yield</code> itself is actually a really simple concept once you understand how blocks work in Ruby.</p> <p>Yes, blocks are a functional programming feature, even though Ruby is not properly a functional language. In fact, Ruby uses the method <code>lambda</code> to create block objects, which is borrowed from Lisp's syntax for creating anonymous functions — which is what blocks are. From a computer science standpoint, Ruby's blocks (and Lisp's lambda functions) are <a href="http://en.wikipedia.org/wiki/Closure_%28computer_science%29" rel="noreferrer">closures</a>. In Ruby, methods usually take only one block. (You can pass more, but it's awkward.)</p> <p>The <code>yield</code> keyword in Ruby is just a way of calling a block that's been given to a method. These two examples are equivalent:</p> <pre><code>def with_log output = yield # We're calling our block here with yield puts "Returned value is #{output}" end def with_log(&amp;stuff_to_do) # the &amp; tells Ruby to convert into # an object without calling lambda output = stuff_to_do.call # We're explicitly calling the block here puts "Returned value is #{output}" end </code></pre> <p>In the first case, we're just assuming there's a block and say to call it. In the other, Ruby wraps the block in an object and passes it as an argument. The first is more efficient and readable, but they're effectively the same. You'd call either one like this:</p> <pre><code>with_log do a = 5 other_num = gets.to_i @my_var = a + other_num end </code></pre> <p>And it would print the value that wound up getting assigned to <code>@my_var</code>. (OK, so that's a completely stupid function, but I think you get the idea.)</p> <p>Blocks are used for a lot of things in Ruby. Almost every place you'd use a loop in a language like Java, it's replaced in Ruby with methods that take blocks. For example,</p> <pre><code>[1,2,3].each {|value| print value} # prints "123" [1,2,3].map {|value| 2**value} # returns [2, 4, 8] [1,2,3].reject {|value| value % 2 == 0} # returns [1, 3] </code></pre> <p>As Andrew noted, it's also commonly used for opening files and many other places. Basically anytime you have a standard function that could use some custom logic (like sorting an array or processing a file), you'll use a block. There are other uses too, but this answer is already so long I'm afraid it will cause heart attacks in readers with weaker constitutions. Hopefully this clears up the confusion on this topic.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload