How to return the adverbial form of a word

I am wondering if there is a method in Ruby for converting a word of type monthto monthly.

Similarly pluralize(word)

+5
source share
1 answer

I don't think there is a built-in method, but you could write a simple one:

CONSONANTS = [ 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x', 'y', 'z' ]

def adverbize(word)
  if word[-2,2] == "ly"
     word
  elsif word.length <= 3 and word[-1] == "y"
    word + "ly"
  elsif word[-2,2] == "ll"
     word + "y"
  elsif CONSONANTS.include? word[-3] and word[-2,2] == "le"
     word.sub(/e$/, "y")
  elsif word[-1] == "y"
     word.chop + "ily"
  else
     word + "ly"
  end
end

Another way to do it that will work every time (this is mostly a joke, but you can use it if you want)

def adverbize(word)
    "In a " + word + " fashion."
end
+7
source

All Articles