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 "bob-J0of1frlU80@public.gmane.org", mail.to_addrs[0].to_s, "mail.to_addrs[0] != @bob-J0of1frlU80@public.gmane.org" 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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Sorry - here''s what I meant to say - what I don''t understand are these lines: identifier = $1 key = $2 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
gene.tani-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org
2007-Sep-04 19:42 UTC
Re: What does this code do?
On Sep 4, 12:28 pm, sydneyos <syd...-+bCDTDb3r/fQT0dZR+AlfA@public.gmane.org> wrote:> Sorry - here''s what I meant to say - what I don''t understand are these > lines: > > identifier = $1 > key = $2http://www.wellho.net/solutions/ruby-ruby-regular-expressions.html --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
$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 &keyuntil 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 On Sep 4, 1:28 pm, sydneyos <syd...-+bCDTDb3r/fQT0dZR+AlfA@public.gmane.org> wrote:> Sorry - here''s what I meant to say - what I don''t understand are these > lines: > > identifier = $1 > key = $2--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 On Tue, 2007-09-04 at 19:23 +0000, sydneyos wrote:> 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 "bob-J0of1frlU80@public.gmane.org", mail.to_addrs[0].to_s, > "mail.to_addrs[0] != @bob-J0of1frlU80@public.gmane.org" > 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? > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---