Search code examples
rubynumbers

ruby number to human-readable string conversion


I need to have a list with id's for each list item being #one, #two etc. Is this the most efficient way or am I missing an in built ruby function here?

-num_array = ["one", "two", "three", "four", "five", "six", "seven"]
-navigation[:primary_level].each_with_index do |primary_item, idx|
   %li{ :id => "#{num_array[idx]}"}

Solution

  • The humanize gem converts digits into words.

    Usage examples:

    2.humanize  # => "two"
    4.humanize  # => "four"
    8.humanize  # => "eight"
    15.humanize # => "fifteen"
    16.humanize # => "sixteen"
    23.humanize # => "twenty three"
    42.humanize # => "forty two"