Using return keyword in Ruby block

Josh M.

I've noticed that if I use the keyword return inside of a block, the entire method returns. I find it confusing that in ruby, you can optionally use the return keyword - it's like: "So what is this thing returning, if anything? Did the developer intend this method to return something?" So I like using return even inside blocks, for example:

def my_method
  items = [ 1, 2, 3 ]

  items_times_ten = items.collect { |o|
    #multiple
    #lines
    #of
    #code ...

    return o * 10  # Ruby exits the method at this point.
  }

  return items_times_ten  #This is never reached.
end

I understand why I don't need to return from the block, but if my block was more intricate, it would seem to help with clarity. My question is, why in the world would Ruby assume that I want to return control out two levels?

If my assumption is incorrect, please correct me. I just want to understand the reasons that control is handled in this way.

PinnyM

Herein lies one of the primary differences between a block/Proc and a lambda/Method (the other primary difference would be arity). If you don't want a call to return to exit the method, that would infer that you expect that block should be self-contained in its flow control, and be treated as an encapsulated method.

This description is essentially what a lambda is - an anonymous method. However, a standard ruby block is essentially an anonymous Proc, and takes nothing away from the flow control of the method.

As mentioned in the comments, you may be able to use next to escape the block without returning control away from the method. 'May' because next may just continue to the next item that the method iterator is passing to the block.

Related to this, see http://yehudakatz.com/2012/01/10/javascript-needs-blocks/ and Differences between Proc and Lambda

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

yield keyword to digest code block like Ruby

From Dev

Will this return keyword exit from the method of if/else block?

From Dev

Using the keyword "new" in a return statement

From Dev

Ruby using regex in select block

From Dev

Using a block to clean map in ruby

From Dev

Pass a ruby &block using rspec

From Dev

Ruby - Array.find, but return the value the block

From Dev

How to return true/false in a block in ruby

From Dev

Using return in try block (Java)

From Dev

Ruby Regexp using gsub is there an equivalent to self keyword?

From Dev

How to grep an xml block in an xml file using a keyword

From Dev

Using `return` in ruby `each` method

From Dev

Using a method return in a string in ruby

From Dev

drying a block using proc in Ruby 2.2

From Dev

Precedence when using `or` vs `||` in Ruby `reject` block

From Dev

Using ARRAYFORMULA and INDIRECT to iterate rows and return as a block

From Dev

Using ARRAYFORMULA and INDIRECT to iterate rows and return as a block

From Dev

why is the statement in finally block executed despite using return in try block

From Dev

why is the statement in finally block executed despite using return in try block

From Dev

Ruby return in yield block called from a method with ensure

From Dev

Print complete text block between two markers using awk, only if the block does not contain a specific keyword

From Dev

`Fault` keyword in try block

From Dev

`Fault` keyword in try block

From Dev

in ruby, when is a block not a block?

From Dev

Using async keyword in method signature to return a Task in Web Api endpoint

From Dev

Ruby - Return duplicates in an array using hashes, is this efficient?

From Dev

Replacing a verbose code block using native methods (Ruby on Rails)

From Dev

Ruby's 'next' keyword

From Dev

Ruby keyword arguments

Related Related

  1. 1

    yield keyword to digest code block like Ruby

  2. 2

    Will this return keyword exit from the method of if/else block?

  3. 3

    Using the keyword "new" in a return statement

  4. 4

    Ruby using regex in select block

  5. 5

    Using a block to clean map in ruby

  6. 6

    Pass a ruby &block using rspec

  7. 7

    Ruby - Array.find, but return the value the block

  8. 8

    How to return true/false in a block in ruby

  9. 9

    Using return in try block (Java)

  10. 10

    Ruby Regexp using gsub is there an equivalent to self keyword?

  11. 11

    How to grep an xml block in an xml file using a keyword

  12. 12

    Using `return` in ruby `each` method

  13. 13

    Using a method return in a string in ruby

  14. 14

    drying a block using proc in Ruby 2.2

  15. 15

    Precedence when using `or` vs `||` in Ruby `reject` block

  16. 16

    Using ARRAYFORMULA and INDIRECT to iterate rows and return as a block

  17. 17

    Using ARRAYFORMULA and INDIRECT to iterate rows and return as a block

  18. 18

    why is the statement in finally block executed despite using return in try block

  19. 19

    why is the statement in finally block executed despite using return in try block

  20. 20

    Ruby return in yield block called from a method with ensure

  21. 21

    Print complete text block between two markers using awk, only if the block does not contain a specific keyword

  22. 22

    `Fault` keyword in try block

  23. 23

    `Fault` keyword in try block

  24. 24

    in ruby, when is a block not a block?

  25. 25

    Using async keyword in method signature to return a Task in Web Api endpoint

  26. 26

    Ruby - Return duplicates in an array using hashes, is this efficient?

  27. 27

    Replacing a verbose code block using native methods (Ruby on Rails)

  28. 28

    Ruby's 'next' keyword

  29. 29

    Ruby keyword arguments

HotTag

Archive