A / บทความใน Ruby [ซ้ำกัน]
Oct 27 2020
มีบางอย่างเช่น [#pluralize ใน ActiveSupport] [1] แต่สำหรับa/ anบทความ?
โดยพื้นฐานแล้วฉันต้องการบางสิ่งเช่น:
'status'.articleize # => "a status"
'urgent'.articleize # => "an urgent"
คำตอบ
2 sergioMAGA Oct 28 2020 at 07:11
คุณสามารถกำหนดString#articleizeให้ส่งคืนบทความภาษาอังกฤษที่เหมาะสม:
class String
def articleize
if self[0] =~ /[aeiou]/i
"an #{self}"
else
"a #{self}"
end
end
end
'status'.articleize # => "a status"
'urgent'.articleize # => "an urgent"
3 Casper Oct 28 2020 at 08:16
ดูเหมือนว่าอัญมณีภาษาศาสตร์อาจเหมาะกับงานนี้ มาลองความท้าทายของ Cary Swoveland:
require 'linguistics/en'
Linguistics.use(:en)
"one-eyed seaman".en.a
=> "a one-eyed seaman"
"honor".en.a
=> "an honor"
# And OP examples...
"urgent update".en.a
=> "an urgent update"
"status update".en.a
=> "a status update"