is there an ordinal to cardinal function in ruby or rails?
Asked Answered
C

3

10

I'm trying to find a better way in expressing my cucumbers so I am looking for an ordinal to cardinal function that converts this:

When I fill up the first passenger field
Then I should see the passenger list update with the first passenger details
When I follow "Add Another Passenger"
Then I should see a second passenger field
When I fill up the second passenger field
Then I should see the passenger list update with the second passenger details

into something more dynamic(instead of creating separate steps for each line)

here's a sample of my web steps

When /^I fill up the first passenger field$/ do
  fill_in("booking_passengers_attributes_0_first_name", :with => "Blah")
  fill_in("booking_passengers_attributes_0_last_name", :with => "blah")
  select("5' to 6'", :from => "booking_passengers_attributes_0_height")
  select("100 to 150lbs", :from => "booking_passengers_attributes_0_weight")
end

When /^I fill up the second passenger field$/ do
  fill_in("booking_passengers_attributes_1_first_name", :with => "Wee")
  fill_in("booking_passengers_attributes_1_last_name", :with => "Sir")
  select("5' to 6'", :from => "booking_passengers_attributes_1_height")
  select("150 to 200lbs", :from => "booking_passengers_attributes_1_weight")
end

See that 0 and 1? I wish to convert "first" to a cardinal number so i can just substitute. You can also just suggest a better way to declare the cukes :)

UPDATED ANSWER I am in the middle of refactoring but basically I used 1st instead of first and used to_i on that.

When /^I fill up the "([^"]*)" passenger field$/ do |id|
  input_id = id.to_i - 1
  fill_in("booking_passengers_attributes_#{input_id}_first_name", :with => id)
  fill_in("booking_passengers_attributes_#{input_id}_last_name", :with => "Passenger")
  select("5' to 6'", :from => "booking_passengers_attributes_#{input_id}_height")
  select("100 to 150lbs", :from => "booking_passengers_attributes_#{input_id}_weight")  
end
Caveman answered 6/5, 2011 at 5:20 Comment(0)
D
7

Use the short-form, easily-parsable ordinal:

When /^I fill up the (\d+)(?:st|nd|rd|th) passenger field$/ do |n|
  # etc...
end
Derby answered 6/5, 2011 at 5:41 Comment(2)
yeah I figured I could use that. much better without the st|nd|rd|th though, i could just use n.to_i and convert it immediately to a number. thanks for the heads up!Caveman
In case anyone else runs into an issue with this, you should use (?:st|nd|rd|th) so that it's not "|th passenger field" (which incorrectly will match "I fill up the 1st")Anticatalyst
P
19

i dont really completely understand,exactly what you want to do, but you can do something like this with active support:

 1.ordinalize    # => "1st"
  2.ordinalize    # => "2nd"
  1002.ordinalize # => "1002nd"

and there is a action view helper number_in_words to get "first" , "second" etc

i don't know much about cukes sorry,

Presignify answered 6/5, 2011 at 5:29 Comment(3)
i wanted something the other way around where i say: something like "first".to_cardinal # => "1" or somethingCaveman
I don't see number_in_words in the docs and it doesn't seem to work for me.Inlaw
ordinalize works like a charm, but I can't find number_in_words anywhere.Botha
D
7

Use the short-form, easily-parsable ordinal:

When /^I fill up the (\d+)(?:st|nd|rd|th) passenger field$/ do |n|
  # etc...
end
Derby answered 6/5, 2011 at 5:41 Comment(2)
yeah I figured I could use that. much better without the st|nd|rd|th though, i could just use n.to_i and convert it immediately to a number. thanks for the heads up!Caveman
In case anyone else runs into an issue with this, you should use (?:st|nd|rd|th) so that it's not "|th passenger field" (which incorrectly will match "I fill up the 1st")Anticatalyst
R
4

This function is built into Chronic:

irb(main):001:0> require 'chronic'
=> true
irb(main):002:0> Chronic::Numerizer.numerize("eighty-fifth").to_i
=> 85
Roesch answered 2/11, 2012 at 20:4 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.