Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix FAQ: block params shadow outer lvars #2851

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

nevans
Copy link

@nevans nevans commented May 21, 2022

This is misleading: "that variable will be modified by the call to the block"

The outer variable isn't mutated; they are different variables. The name points to one variable outside the block and a different variable inside the block. I'm not sure if my proposed change is better though! 😉

Also, I moved the "arguments are passed" paragraph to the top. The original ordering was fine: it's reasonable to describe how to define block parameters first, and then how to pass arguments in them. But it seems like reversing the order is the most direct answer to the question, as the question is worded. Perhaps the original order was better, but we should add a second question heading: "How do I define a block's parameters"

@nevans nevans requested a review from a team as a code owner May 21, 2022 17:03
@bo-tato
Copy link

bo-tato commented Aug 8, 2023

I also got confused by this when reading through the FAQ. I asked on reddit and someone pointed out that before ruby 1.9.1 modifying a proc parameter would modify the outer variable of the same name, so it seems this section of the FAQ was written before then and hasn't been updated

@@ -361,19 +361,20 @@ the method is invoked. It is evaluated using the scope of the method.

### How do I pass arguments to a block?

Typically, arguments are passed to a block using `yield` (or an iterator
that calls `yield`), or by using the `Proc.call` method.
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had previously just moved this paragraph to the top, but it should also be altered to use the appropriate syntax to indicate an instance method on Proc. I prefer the # syntax, but this should use whichever is most consistent with other parts of this FAQ.

Suggested change
that calls `yield`), or by using the `Proc.call` method.
that calls `yield`), or by using the `proc.call` method.
Suggested change
that calls `yield`), or by using the `Proc.call` method.
that calls `yield`), or by using the `Proc#call` method.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants