2011-05-06 18 views
10

我試圖找到我的表達黃瓜更好的辦法,所以我想找一個序號,以基數函數轉換這樣的:有沒有在紅寶石或鐵軌的基本功能的序數?

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 

到的東西更多動態(而不是創建每行單獨的步驟)

,這裏是我的網絡的樣本步驟

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 

看到0和1?我希望將「第一」轉換爲基數,以便我可以替換。你也可以提出一個更好的方式來聲明

修訂ANSWER 我在重構的中間:)但基本上我用1號,而不是第一和使用to_i上的cukes。

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 

回答

7

使用短形式,易於解析的順序:

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

是的我想我可以使用它。如果沒有stdnd | rd | th,我可以使用n.to_i並立即將其轉換爲數字。感謝您的支持! – corroded 2011-05-06 05:42:37

+1

如果有其他人遇到這個問題,你應該使用(?:st | nd | rd | th),以便它不是「| th passenger field」(錯誤地匹配「我填滿第一個」) – 2011-12-02 23:21:28

19

我真的不完全理解,你想要做什麼,但你可以做這樣的事情有積極的支持:

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

,有一個動作視圖助手number_in_words拿到「第一「,‘第二’等

我不知道很多關於cukes對不起,

+0

我想要的東西圍繞在這裏我說的其他方式:像「第一次」 .to_cardinal#=>「1」或什麼 – corroded 2011-05-06 05:35:48

+0

哦確定,對不起我沒有幫助 – loosecannon 2011-05-07 23:57:26

+1

我沒有在文檔中看到'number_in_words',它似乎不適用於我。 – Dex 2014-04-05 22:07:20

4

該功能內置到Chronic

irb(main):001:0> require 'chronic' 
=> true 
irb(main):002:0> Chronic::Numerizer.numerize("eighty-fifth").to_i 
=> 85