googleads
Active Support Core Extensions in Rails | Hire Ruby On Rails Developers
preloder
Ruby on Rails Web Development

Active Support Core Extensions In Rails

In my previous blog, we have discussed about Active Jobs In Rails – Ruby On Rails Guide and today am planning to go one step ahead and like to discuss about Active Support Core Extensions In Rails. Will dig into every methods and it’s benefits so hope you will enjoy it. Before all, let me give you a quick view of Active support in Rails, Well, Active support is the collection of component and standard library extensions which is responsible of providing multiple supports to ease the development of Rails application. It’s offers bundle of default methods which will help us throughout the development life cycle of Ruby on Rails Applications. Without limiting it’s good fortune, Active support also provides extensions for strings, integers, arrays, date, enums and lot more. So let’s take a look at the list of methods and each of it’s benefits one by one,

1) remove(parameter)

 

  • This method takes substring as an argument and removes all the occurrences in the string.
  • We can also pass pattern as an argument

 

Example:

 

2) squish

 

  • This method replaces multiple white spaces with single white space
  • It works for both ASCII and Unicode whitespace.

Example:

 

3) truncate_words(parameter)

 

  • This method returns the copy of the string words based on the integer we passed in the parameter.
  • We can also pass a separator to truncate string at natural break, truncate_words(1, separator: ‘’)

Example:

 

4) Inquiry

 

  • This method used to check the string equality and if matches it returns true else will return the false.
  • It converts a string into StringInquirer object and checks for equality

Example:

 

Related: Active Jobs In Rails – Ruby On Rails Guide

 

5) starts_with?(parameter)

 

  • If the string starts with specified substring then it will return true and if the string doesn’t starts with the specified substring then it will return false.

Example:

 

6) ends_with?(parameter)

 

  • If the string ends with specified substring then it will return  true else it will return false.

Example:

 

7) at(parameter)

 

  • This method returns character of a string based on the position we passed in the method.
  • It will return nil if the position doesn’t match.

Example:

 

8) from(parameter)

 

  • This method returns substring of a string based on position we passed.
  • It takes position as an argument and returns all the substring from that position.

Example:

 

Best To Read: Active Storage In Rails 5.2 With Real Time Example

 

9) to(parameter)

 

  • This method returns substring of a string, it takes position as an argument and returns substring from the beginning to til the position we specified.

Example:

 

10) pluralize(params)

 

  • This method returns plural of the string
  • We can also pass count parameter, if count is 1 – returns singular form and if the count is not 1 – it will return the plural form

Example:

 

11) Singularize

 

  • This method returns singular form of the string

Example:

 

12) camelize(parameter)

 

  • This method returns string in Camel Case
  • We can also pass lower and upper as arguments to the camelize, by default it will take upper. Here the lower will return first letter as small and the remaining as Upper letter.

Example:

 

13) underscore

 

  • It is the viceversa of camelize but it downcases the string

Example:

 

14) titleize

 

  • This method returns capitalized words of the string
  • titleize is aliased to titlecase

Example:

  • “welcome to ruby on rails”.titleize

=> “Welcome To Ruby On Rails”

15) dasherize

 

  • This method replaces the underscores with dashes.

Example:

 

16) parameterize(parameter)

 

  • This method normalize the string so that we can use it in URLs to make the url’s pretty
  • To preserve the string case, use preserve_case: true and by default it’s set to false.

Example:

 

17) tableize

 

  • This method returns string with underscore and pluralize.

Example:

 

18) Classify

 

  • This method is an inverse of tableize
  • It returns class name of a string

Example:

 

19) humanize:

This method does following things

  • Deletes underscores from the string and replaces them with spaces
  • Removes _id suffix
  • Down-cases all words other than acronyms
  • Capitalize first word

Example:

 

20) foreign_key

 

  • This method returns foreign key from a class name.
  • It adds underscores and Id suffix.

Example:

 

21) to_date

This method converts string to date

Example:

 

22) to_time

 

  • This method converts string into time.

Example:

 

23) to_datetime

 

  • This method converts string into date & time,

Example:

 

24) Phone:

 

  • Returns telephone number in string format
  • We can also pass area_code: true –  which formats the area code in brackets
  • We can also pass country_code: 91 – which formats country code with +91

Example:

 

25) currency:

 

  • Returns currency in string format

Example:

 

26) percentage:

 

  • Returns percentage in string format

Example:

 

27) delimited:

 

  • Returns delimited form of string

Example:

 

28) rounded:

 

  • Returns rounded string.

Example:

 

29) human_size: 

 

  • Returns string as a human readable bytes in number.

Example:

 

30) human:

 

  • Returns number in human readable string format

Example:

 

31) multiple_of?(parameter)

 

  • Tests whether the specified argument is multiple of integer or not and based on that it returns true or false

Example:

 

32) ordinal:

 

  • Returns ordinal suffix string of provided integer

Example:

 

33) ordinalize:

 

  • This method returns ordinalized string of provided integer

Example:

 

34) sum

 

  • This method returns sum of the numbers or strings

Example:

 

35) many?:

 

  • This is the shorthand for collection.size > 1

Example:

 

36) without(parameter)

 

  • This method returns the copy of an element and it will remove the items whatever we wanted to remove. (Note: this method will work for latest versions of Ruby)

Example:

 

37) to(parameter)

 

  • This method returns sub array of an array and it returns sub array from 0 to provided index.

Example:

 

38) from(parameter)

 

  • This method returns sub array of an array, and it returns sub array from provided index to last element.

Example:

 

We can also get array element by calling element position with words

For Example:

 

OK guys, mostly i have covered all the methods here and also let us know your best methods you’re comfortable with, share your best practices to the world, let everybody know everything about Rails. Thanks for reading and if you find it helpful then do share with your friends, also don’t forget to  subscribe us to get more blogs on all technologies.

The following two tabs change content below.

Venkata Krishna

An ideal full stack software developer having 2 years of experience in Web app development and expertise in Ruby on Rails, CMS services. Let it be a technology or charity he never fails to feed the needy. An honest individual who continuously involves himself in creating Social Awareness as part of his life goals.

One thought on “Active Support Core Extensions In Rails”

Leave a Reply

Your email address will not be published. Required fields are marked *

[contact-form-7 id="120788" title="Web Page Form"]

Schedule Your Call