At Agira, Technology Simplified, Innovation Delivered, and Empowering Business is what we are passionate about. We always strive to build solutions that boost your productivity.

,

Active Support Core Extensions In Rails

  • By Venkata Krishna
  • September 26, 2018
  • 2057 Views

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:

"Ruby on Rails Ruby on Rails".remove("Ruby")
=> " on Rails  on Rails"
"Ruby on Rails Ruby on Rails".remove(/Ruby /)
=> "on Rails on Rails"

 

2) squish

 

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

Example:

"  Ruby   on Rails   Ruby on Rails ".squish
       => "Ruby on Rails Ruby on Rails"
" \n  Ruby\n\r \t on   Rails \n".squish
      =>  "Ruby on Rails"

 

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:

"Ruby on Rails Ruby on Rails".truncate_words(2)
       => "Ruby on..." #2 words
"Ruby on Rails Ruby on Rails".truncate_words(3)
      => "Ruby on Rails..." # 3 words

 

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:

"Rails".inquiry.Rails?
=> true
"Rails".inquiry.Ruby?
=> false

 

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:

"Ruby on Rails".starts_with?("Ruby")
=> true
"Ruby on Rails".starts_with?("Rubyq")
=> false

 

6) ends_with?(parameter)

 

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

Example:

"Ruby on Rails".ends_with?("Rails")
=> true
"Ruby on Rails".ends_with?("RailsW")
=> false

 

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:

"Ruby on Rails".at(1)
=> "u"
"Ruby on Rails".at(5)
=> "o"
"Ruby on Rails".at(15)
=> nil

 

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:

"Ruby on Rails".from(0)
=> "Ruby on Rails"
"Ruby on Rails".from(10)
=> "ils"
"Ruby on Rails".from(100)
=> nil

 

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:

"Ruby on Rails".to(5)
=> "Ruby o"
"Ruby on Rails".to(3)
=> "Ruby"
"Ruby on Rails".to(100)
=> "Ruby on Rails"

 

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:

"book".pluralize
=> "books"
"person".pluralize
=> "people"
"person".pluralize(0)
=> "people"
"person".pluralize(1)
=> "person"

 

11) Singularize

 

  • This method returns singular form of the string

Example:

"people".singularize
=> "person"
"books".singularize
=> "book"
"person".singularize
=> "person"

 

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:

"Hello_world".camelize
=> "HelloWorld"
"Ruby_on_rails".camelize
=> "RubyOnRails"
"user/session".camelize
=> "User::Session"
"ruby_on_rails".camelize(:lower)
=> "rubyOnRails"

 

13) underscore

 

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

Example:

"HelloWorld".underscore
=> "hello_world"
"RubyOnRails".underscore
=> "ruby_on_rails"

 

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:

"ruby_on_rails".dasherize
=> "ruby-on-rails"

 

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:

"Agira Technologies".parameterize
=> "agira-technologies"
"Agira Technologies".parameterize
=> "Agira-Technologies"

 

17) tableize

 

  • This method returns string with underscore and pluralize.

Example:

"Invoice".tableize
=> "invoices"
"MyInvoice".tableize
=> "my_invoices"

 

18) Classify

 

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

Example:

"invoices".classify
=> "Invoice"
"my_invoices".classify
=> "MyInvoice"

 

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:

"user".humanize
=> "User"
"user_data".humanize
=> "User data"
"user_id".humanize
=> "User"

 

20) foreign_key

 

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

Example:

"Book".foreign_key
=> "book_id"
"Article::Comment".foreign_key
=> "comment_id"

 

21) to_date

This method converts string to date
Example:

"2018-09-25".to_date
=> Tue, 25 Sep 2018

 

22) to_time

 

  • This method converts string into time.

Example:

"2018-09-25 10:24:45".to_time
=> 2018-09-25 10:24:45 +0530

 

23) to_datetime

 

  • This method converts string into date & time,

Example:

"2018-09-25 10:24:45".to_datetime
=> Tue, 25 Sep 2018 10:24:45 +0000

 

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:

9876543210.to_s(:phone)
=> "987-654-3210"
9876543210.to_s(:phone, area_code: true)
=> "(987) 654-3210"
919876543210.to_s(:phone, country_code: 91)
=> "+91-91987-654-3210"

 

25) currency:

 

  • Returns currency in string format

Example:

500000.to_s(:currency)
=> "$500,000.00"

 

26) percentage:

 

  • Returns percentage in string format

Example:

100.to_s(:percentage)
=> "100.000%"
100.to_s(:percentage, precision: 0)
=> "100%"

 

27) delimited:

 

  • Returns delimited form of string

Example:

54321.45.to_s(:delimited)
=> "54,321.45"

 

28) rounded:

 

  • Returns rounded string.

Example:

100.344.to_s(:rounded, precision: 0)
=> "100"
100.344.to_s(:rounded, precision: 1)
=> "100.3"

 

29) human_size:

 

  • Returns string as a human readable bytes in number.

Example:

100.to_s(:human_size)
=> "100 Bytes"
1000.to_s(:human_size)
=> "1000 Bytes"
10000.to_s(:human_size)
=> "9.77 KB"
100000.to_s(:human_size)
=> "97.7 KB"
1000000.to_s(:human_size)
=> "977 KB"

 

30) human:

 

  • Returns number in human readable string format

Example:

1000000.to_s(:human)
=> "1 Million"
10000.to_s(:human)
=> "10 Thousand"
1000.to_s(:human)
=> "1 Thousand"

 

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:

10.multiple_of?(2)
=> true
10.multiple_of?(3)
=> false

 

32) ordinal:

 

  • Returns ordinal suffix string of provided integer

Example:

1.ordinal
=> "st"
2.ordinal
=> "nd"
3.ordinal
=> "rd"

 

33) ordinalize:

 

  • This method returns ordinalized string of provided integer

Example:

1.ordinalize
=> "1st"
2.ordinalize
=> "2nd"
3.ordinalize
=> "3rd"

 

34) sum

 

  • This method returns sum of the numbers or strings

Example:

[2,4,5].sum
=> 11
(1..5).sum
=> 15
%w(welcome to ruby on rails).sum
=> "welcometorubyonrails"

 

35) many?:

 

  • This is the shorthand for collection.size > 1

Example:

<% if books.many? %>
--- code ---
<% end %>
books = [1,3,3]
books.many?
=> true
books = [1]
books.many?
=> false
books = []
books.many?
=> false

 

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:

books = ["Ruby", "Rails", "Ruby on Rails", "Rails Guide"]
books.without("Ruby", "Rails")
=> ["Ruby on Rails", "Rails Guide"]

 

37) to(parameter)

 

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

Example:

[1,2,3,45,6].to(1)
=> [1, 2]
[1,2,3,45,6].to(100)
=> [1, 2, 3, 45, 6]
[1,2,3,45,6].to(3)
=> [1, 2, 3, 45]

 

38) from(parameter)

 

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

Example:

[1,2,3,45,6].from(1)
=> [2, 3, 45, 6]
[1,2,3,45,6].from(3)
=> [45, 6]

 
We can also get array element by calling element position with words
For Example:

[1,2,3,45,6].first
=> 1
[1,2,3,45,6].fourth
=> 45
[1,2,3,45,6].tenth
NoMethodError: undefined method `tenth' for [1, 2, 3, 45, 6]:Array

 
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.