Sha256: edb49ba669eeb69b7a6a22d88dc184ffe6fa031a0bae5945a556f252231cd7e2

Contents?: true

Size: 855 Bytes

Versions: 6

Compression:

Stored size: 855 Bytes

Contents

module Cape

  # Provides utility functions.
  module Util

    # Conditionally transforms the specified _noun_ into its plural form.
    #
    # @param [String] noun  a singular noun
    # @param [Fixnum] count the quantity of _noun_; optional
    # @return [String] the plural of _noun_, unless _count_ is +1+
    def self.pluralize(singular_noun, count=2)
      return singular_noun if count == 1

      "#{singular_noun}s"
    end

    # Builds a list phrase from the elements of the specified _array_.
    #
    # @param [Array of String] array zero or more nouns
    # @return [String] the elements of _array_, joined with commas and "and", as
    #                  appropriate
    def self.to_list_phrase(array)
      return array.join(' and ') if (array.length <= 2)

      [array[0...-1].join(', '), array[-1]].join ', and '
    end

  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
cape-1.4.0 lib/cape/util.rb
cape-1.3.0 lib/cape/util.rb
cape-1.2.0 lib/cape/util.rb
cape-1.1.0 lib/cape/util.rb
cape-1.0.3 lib/cape/util.rb
cape-1.0.2 lib/cape/util.rb