-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
--- | ||
title: Meeting some Locals | ||
date: 2015-05-14 | ||
tags: syntax, regex | ||
--- | ||
|
||
There are two very different ways to create local variables in Ruby. You are probably familiar with the classical way: | ||
|
||
a, b = "$", "€" | ||
a # => "$" | ||
b # => "€" | ||
|
||
It is simple to understand and looks good. But Ruby would not be Ruby, if there weren"t for more obscure ways to assign variables: You could rewrite the previous example to create local variables in a more subtle way: | ||
|
||
%r<(?'a'.)(?'b'.)>=~"$" "€" | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
janlelis
Author
Owner
|
||
a # => "$" | ||
b # => "€" | ||
|
||
|
||
This implicit creation of local variables is not recommended, because obviously, it violates **PrOWCoFoHuNoMa** (Principle of writing code for humans, not machines). | ||
|
||
By the way, this will not work, when you swap operands: | ||
|
||
"$" "€"=~%r<(?<a>.)(?<b>.)> | ||
a # NameError: ... | ||
|
||
## One More Option | ||
|
||
There is also a third way to set local variables: binding's `local_variable_set`, but it does not really count, since you cannot introduce new variables this way: | ||
|
||
a = nil | ||
binding.local_variable_set :a, "$" | ||
binding.local_variable_set :b, "€" | ||
a # => "$" | ||
b # NameError: ... | ||
|
||
## Resources | ||
- [RDoc: Regexp#=~](http://ruby-doc.org/core-2.2.2/Regexp.html#method-i-3D-7E) | ||
- [RDoc: Binding#local_variable_set](http://ruby-doc.org/core-2.2.2/Binding.html#method-i-local_variable_set) |
Is this supposed to look extra obfuscated? If not, why not replace with
?