【问题标题】:is there an ordinal to cardinal function in ruby or rails?ruby 或 rails 中是否有序数到基数函数?
【发布时间】:2011-05-06 05:20:23
【问题描述】:

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

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

更新答案 我正在进行重构,但基本上我使用了 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

【问题讨论】:

    标签: ruby-on-rails ruby cucumber cardinality ordinals


    【解决方案1】:

    我不太明白,到底你想做什么,但你可以在积极的支持下做这样的事情:

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

    还有一个动作视图助手 number_in_words 来获取 "first" 、 "second" 等

    对不起,我对cukes了解不多,

    【讨论】:

    • 我想要的东西与我所说的相反:像 "first".to_cardinal # => "1" 之类的东西
    • 我在文档中没有看到 number_in_words,它似乎对我不起作用。
    • ordinalize 就像一个魅力,但我在任何地方都找不到number_in_words
    【解决方案2】:

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

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

    【讨论】:

    • 是的,我想我可以使用它。不过,如果没有 st|nd|rd|th 会更好,我可以使用 n.to_i 并立即将其转换为数字。感谢您的提醒!
    • 如果其他人遇到这个问题,您应该使用 (?:st|nd|rd|th) 以便它不是“|th 乘客字段”(错误地匹配“I填满第一个")
    【解决方案3】:

    这个函数内置在Chronic中:

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

    【讨论】:

      猜你喜欢
      • 2014-03-26
      • 2011-01-27
      • 2010-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-01
      • 2011-08-20
      相关资源
      最近更新 更多