开发者

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

开发者 https://www.devze.com 2023-03-03 01:01 出处:网络
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:

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("开发者_StackOverflow中文版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


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,


Use the short-form, easily-parsable ordinal:

When /^I fill up the (\d+)(?:st|nd|rd|th) passenger field$/ do |n|
  # etc...
end


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
0

精彩评论

暂无评论...
验证码 换一张
取 消