Ruby의 A / An 기사 [중복]

Oct 27 2020

[#pluralize in 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

Linguistics gem 이 작업에 적합 할 것 같습니다 . 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"