ruby或rails中是否有序数到基数函数?

发布于 2024-11-05 21:02:27 字数 1792 浏览 5 评论 0原文

我正在尝试找到一种更好的方式来表达我的黄瓜,所以我正在寻找一个序数到基数函数,将其转换

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?我希望将“第一个”转换为基数,这样我就可以替换。您也可以建议一种更好的方式来声明 cukes :)

更新的答案 我正处于重构过程中,但基本上我使用了 1st 而不是first,并使用了 to_i 。

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-11-12 21:02:27

我并不完全理解,确切地说你想要做什么,但是你可以在积极支持的情况下做这样的事情:

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

并且有一个动作视图助手 number_in_words 来获取“第一”,“第二”等

我对 cukes 不太了解对不起,

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,

一抹苦笑 2024-11-12 21:02:27

使用简短、易于解析的序数词:

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

Use the short-form, easily-parsable ordinal:

When /^I fill up the (\d+)(?:st|nd|rd|th) passenger field$/ do |n|
  # etc...
end
你的往事 2024-11-12 21:02:27

此函数内置于 Chronic 中:

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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文