Member-only story

Frequently used Active Support Methods in Ruby on Rails — Part 2

Gokul
2 min readJan 30, 2021

--

truncate_words

The method truncate_words returns a given text after a given number of words. Here is the example for the truncate_words with word count argument.

'Once upon a time in a world far far away'.truncate_words(4)# => "Once upon a time..."

Its also supports the option parameter separator as a string or regexp to specify a different separator of words:

'Once<br>upon a<br>time<br>in<br>a<br>world'.truncate_words(5, separator: '<br>')# => "Once<br>upon a<br>time<br>in<br>a..."

The last characters will be replaced with the omission string (defaults to
“…”)

'And they found that many people were sleeping better.'.truncate_words(5, omission: '... (continued)')

# => "And they found that many... (continued)"

camelize or camelcase

By default, camelize/camelcase converts strings to UpperCamelCase. If the argument to camelize is set to lower then camelize produces lowerCamelCase. camelize will also convert ‘/’ to ‘::’ which is useful for converting paths to…

--

--

Gokul
Gokul

No responses yet