# What does this code do?

**URL:** <https://www.ruby-forum.com/t/what-does-this-code-do/113777>\
**Category:** Rails\
**Created:** [September 4, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/what-does-this-code-do/113777 "2007-09-04T21:23:57Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sydneyos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sydneyos](https://www.ruby-forum.com/u/sydneyos)\
**Post date:** [September 4, 2007, 9:23pm UTC](https://www.ruby-forum.com/t/what-does-this-code-do/113777/1 "2007-09-04T21:23:57Z")

</div>

Here’s a newbie question. Am looking at the do\_forgot\_password test  
method in SaltedHashLoginGenerator and it is failing in the final  
assert listed here. What I don’t understand at all are the lines.  
Actually, I also don’t understand how a call to change\_password could  
work if the user isn’t logged in (as in this test case), but that’s  
another issue.

identifier = $1  
key = $2

What are $1 and $2? Where do they come from?

assert\_equal 1, ActionMailer::Base.deliveries.size, “ActionMailer  
deliveries size not 1; flow is logged\_in = false”  
mail = ActionMailer::Base.deliveries[0]  
assert\_equal “removed\_email\_address@domain.invalid”, mail.to\_addrs[0].to\_s,  
“mail.to\_addrs[0] != @removed\_email\_address@domain.invalid”  
mail.encoded =~ /user[identifier]=(._?)&key=(._?)"/  
identifier = $1  
key = $2  
post :change\_password, “user” =\> { “password” =\> “#{password}”,  
“password\_confirmation” =\> “#{password}”, “identifier” =\>  
“#{identifier}” }, “key” =\> “#{key}”  
assert\_session\_has “user” #Fails here  
get :logout

Anybody?

---

<div class="post-metadata">

**Author:** ![sydneyos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sydneyos](https://www.ruby-forum.com/u/sydneyos)\
**Post date:** [September 4, 2007, 9:29pm UTC](https://www.ruby-forum.com/t/what-does-this-code-do/113777/2 "2007-09-04T21:29:57Z")

</div>

Sorry - here’s what I meant to say - what I don’t understand are these  
lines:

identifier = $1  
key = $2

---

<div class="post-metadata">

**Author:** ![sydneyos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sydneyos](https://www.ruby-forum.com/u/sydneyos)\
**Post date:** [September 4, 2007, 9:44pm UTC](https://www.ruby-forum.com/t/what-does-this-code-do/113777/3 "2007-09-04T21:44:06Z")

</div>

On Sep 4, 12:28 pm, sydneyos [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Sorry - here’s what I meant to say - what I don’t understand are these  
> lines:
> 
> identifier = $1  
> key = $2

> **[Ruby Regular Expressions](http://www.wellho.net/solutions/ruby-ruby-regular-expressions.html)**
>
> If you want to say does this string

---

<div class="post-metadata">

**Author:** ![sydneyos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sydneyos](https://www.ruby-forum.com/u/sydneyos)\
**Post date:** [September 4, 2007, 9:49pm UTC](https://www.ruby-forum.com/t/what-does-this-code-do/113777/4 "2007-09-04T21:49:11Z")

</div>

Hi

They come from the regular expression above them

mail.encoded =~ /user[identifier]=(._?)&key=(._?)"/

$1 refers to the first match in the regex and $2 to the second, where it  
gives (.\*?). They are being stored away for further use later in the  
script. It might help you to read up on the match operator =~.

HTH

Clive

---

<div class="post-metadata">

**Author:** ![sydneyos](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/f4b2a3/32.png) [@sydneyos](https://www.ruby-forum.com/u/sydneyos)\
**Post date:** [September 4, 2007, 10:37pm UTC](https://www.ruby-forum.com/t/what-does-this-code-do/113777/5 "2007-09-04T22:37:31Z")

</div>

$1 and $2 are variables that hold the captured information in the  
regular expression on the previous line:  
mail.encoded =~ /user[identifier]=(._?)&key=(._?)"/  
Translation:  
All the characters between user[identifier]= and the first occurrence  
of &key= are captured into $1, and all the characters after &key=  
until the first occurrence of " are captured into $2. If that barely  
made sense to you, I’d recommend reading a regular expression tutorial  
(like on regular-expressions.info). You will likely use them often if  
you plan to do any significant amount of parsing in just about any  
language (esp. Ruby, Perl, Python, JavaScript, PHP, and so on).

HTH,  
Matt
