-
Notifications
You must be signed in to change notification settings - Fork 553
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
gsub_file with block, no access to $1 etc? #207
Comments
As far as I know, we cannot access to Actually, it was discussed by ruby core team here (, although it is in Japanese), |
Weird, special deal of $1 etc I guess. Thanks for confirming. Problem is that this makes the block form of gsub_file not all that useful, yeah? Yeah, if block form of gsub gave you a MatchData, that would certainly solve things. Hmm, I wonder, should thor gsub file use some other implementation, such that it can grab a MatchData and pass it to thor.gsub_file's own block, so the block can have access to the paren groups etc? Hmm, not sure there's any clean way to do that though, bah. |
I'm not sure which way is the best. Just an idea, but one of the ways is this:
the above two lines will get same results. Also you can use the old style with a single block parameter, such as:
However, the following are still not accessible:
What do you think? |
I like it! Weird that the block has no access to Is it backwards compat with old use? Appears so as you didn't remove/change any existing tests? Neither of us are committers on thor though, huh? Wonder how we get the attention of someone who is, on this proj that doesn't have too much maintainer attention at the moment. You're not a committer on |
Ah yes, I see $& is the backwards-compat one. And can I use |
I am not good at English, so I might miss the point. You can access to $ Because, I think, a block drag the scope where the block was written. Ruby core team discussed current block parameter of gsub! is not so useful and we sometimes have to use global variables such as $1 etc, and should change block parameter of gsub! to more useful one. But, the change of block parameter is definetely backwards incompatible and it is still pending. Maybe, in the future, the block parameter of gsub! might be changed. So the situation is we can write wrapping methods of String#gsub! which passes MatchData as a block parameter. I wonder which is the best block parameter of Actions#gsub_file
I am not a committer but I want to send a pull request if I see the best block parameter. |
Ah, I understand, thanks! I think this would be a good change to thor, personally. I think your Maybe we can attract the attention of a committer. On Jan 29, 2012, at 5:10 PM, Kentaro Imai wrote:
|
Weird, in 1.8.7 it works fine, no? Wonder why ruby core team doesn't just make it work like it did in 1.8.7 |
OK. I'll send a pull request. |
See pull-req #217 |
This is definitely broken as near as I can tell. |
Yeah, this would still be useful. Any reason why it never made it in? It looks like the pull request was closed without any comment. |
When using gsub_file action with a block, if the expression matched had paren grouping, it seems you ought to be able to access $1 etc in the block, to construct your replacement string.
Looking at the source code, it still seems like this should work -- the block passed in to gsub_file is passed to ordinary String#gsub!, which definitely makes $1 etc available in the block.
And yet, I can not get this to work. (0.14.6, in context of a Rails 3.1 generator). $1 is always nil, even if there was a matching paren group in the regexp.
Anyone have any idea what's going on?
The text was updated successfully, but these errors were encountered: