What does this code do?

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 “[email protected]”, 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?

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

identifier = $1
key = $2

On Sep 4, 12:28 pm, sydneyos [email protected] wrote:

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

identifier = $1
key = $2

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

$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