Ruby: regex in case statements

I am new to Ruby. I am trying to figure out how to write a case nested argument. Here is an example of what I'm trying to do:

# cucumber, webrat paths.rb def path_to(page_name) case page_name when /the "?(.*)"? event registration homepage/ then case $1 when '2011 CIO GL Global' then '/event/index/id/236' when '2011 CIO IS Chicago' then '/event/index/id/275' when '2011 CIO ES Denver' then '/event/index/id/217' when '2011 CIO ES Vancouver, BC' then '/event/index/id/234' else raise "Can't find mapping for \"#{$1}\" to a path.\n" + "Now, go and add a mapping in #{__FILE__}" end when /the contact match verification page/ '/attendee/user-verification' end end 

My properties file says:

 When I go to the "2011 CIO IS Chicago" event registration homepage 

It does not work at this step because it raises the exception mentioned above, even if it is defined in my case above. What am I doing wrong?

+4
source share
5 answers

Yes, the second question mark mixes the regular expression.

Expression:

 .*b? 

can match the string "axb" in two ways: β€’ with the coincidence of the whole thing or with. * coincidence of "ax" and "b?"; matching "b". The regex algorithm is β€œgreedy” - it prefers to match as much as it can, as soon as it can.

I would rewrite the regex as:

  when /the "?([^"]*)"? event registration homepage/ then 

to make sure that this $ 1 does not end with any double quotes in it ...

+4
source

This works for me. How do you test it?

Update: Aha, this is because your regex matches the trailing quote, so $ 1 ends with a quote that is missing in your inner case.

There are several ways to fix this.

  • Can you match "?[^"]*"?
  • The selectors for the inner case can simply end in ..."?'
  • Can you use an unwanted match: "?(.*?)"? .
+1
source

Change

  when /the "?(.*)"? event registration homepage/ then 

to

  when /the "?(.*)" event registration homepage/ then 
+1
source

The problem is in your regular expression. Try to delete the last ? because it calls the option, and the greedy search up (.*) puts it in a $1 match.

Note:

 > s = 'the "2011 CIO IS Chicago" event registration homepage' > /the "?(.*)"? event registration homepage/.match s #=> <MatchData "the \"2011 CIO IS Chicago\" event registration homepage" 1:"2011 CIO IS Chicago\""> > $1 #=> "2011 CIO IS Chicago\"" > /the "?(.*)" event registration homepage/.match s #=> #<MatchData "the \"2011 CIO IS Chicago\" event registration homepage" 1:"2011 CIO IS Chicago"> > $1 #=> "2011 CIO IS Chicago" 

Edit: if you want " be optional, you probably need to do something like: (?:"(.*)"|(.*)) . And then you will need to use nil guard to find out which link is returned. $1 || $2 .

0
source

I would approach this a little differently. In your cucumber function, I suggest changing the wording to:

 When I go to the registration homepage for the event named "2011 CIO IS Chicago" 

And in your paths.rb file paths.rb I would handle all events with one regex, for example:

 when /the registration homepage for the event named "?(.*)"?/ then event = Event.find_by_name($1) raise "could not find an event with name: #{$1}" if event.blank? event_path(event) # or create the route manually like so: # "event/index/id/#{event.id}" 

It depends on your Event model, which has a method that can find an event with its name (in this case, I assumed find_by_name ), and a resourceful route is configured for :events .

0
source

Source: https://habr.com/ru/post/1345613/


All Articles