Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] Yield statement

I've searched the forums, with no result in a help topic about this. I read through the stuff about yield in the RMXP help file, but it didn't bring me much closer to what it actually does.

If I make a method like this:
Code:
def something
   yield 5
end
What would happen if I ran the method? Would it return anything? Any detailed explanation on how yield works, where you would use it and what it's good for is greatly appreciated.

Thanks in advance.
 

poccil

Sponsor

The "yield" command calls a block of code passed to the method with the given parameters.

In your case, if a block of code was passed to the method "something", that block would be called and would receive the number 5.  Here's an example of code that calls "something" and passes a block of code:

Code:
something() {|parameter|
  p(parameter)
}

This code would show the number 5 on the screen.  Note the syntax "|parameter|".  That identifies the name of the parameter that the block of code receives.  Note also that the block of code itself is wrapped in braces.  There is however, an alternative syntax for blocks:

Code:
something() do |parameter|
  p(parameter)
end

Here, "do" and "end" are the block's delimiters.

I hope this helps.
 
So basically, it returns an iterator which you set to a variable called parameter using |*|?

So would it be possible to do:
Code:
something() {|a| var = a}
To make 5 being set to 'var'?
 

poccil

Sponsor

Yes, that's possible.  However, the variable "var" is within the scope of the block.  If the result of var is needed elsewhere, it must be declared outside of the block, like this:

Code:
var = 2
something() {|a|  var = a }
# var is now 5
 
New question! Still about yield, just wondering, why and when would we actually use this? I mean, wouldn't it be easier to use return? Doesn't it do the same thing, just simpler? I guess not, since this actually is there, but when would we use it then? For what purposes? Is it because yield can provide more return values, and doesn't exit the method instantly, or something like that?

Thanks in advance.
 

Zeriab

Sponsor

You can use the yield command to call a Proc anywhere in the method and have computation afterwards.
In addition you can yield several times. I.e. you can call the given Proc with various arguments.

The return command stops the method and returns a value.

To show you a case where using returns is more bothersome than yielding values look at this excerpt from my Linked_List class
Code:
  #--------------------------------------------------------------------------
  # * Name      : Each
  #   Info      : The each method required by the Enumerable module
  #   Author    : Zeriab
  #   Call Info : Refer to the documentation on the Enumerable module
  #--------------------------------------------------------------------------
  def each
    # Return if there are no elements in the list
    return if self.head.nil?
    # Yield the first value of the list (The head)
    element = self.head
    yield element.value
    # Run through the list until the tail is fount or the next element of the
    # list is nil
    while element != self.tail && element.next_element != nil
      # Retrieve and yield the next element in the list
      element = element.next_element
      yield element.value
    end
  end

This yields each value in the linked list. Basically the given piece of code is executed on each value in the list.
How would you do this with returns? How should you alter the code that calls the .each?
I hope this will help you understand more about yields and returns. I can go more in depth into how it works, but I won't do so if you are able to see where I am getting at with this.

In addition you can google Visitor Design Pattern to help you understand how the principle behind the .each method and how passing blocks and yielding helps in this aspect.

*hugs*
- Zeriab
 

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top