Ruby's Gsub method usage

Hi, This is Shubham Mishra a ROR, iOS, Python developer,  this is the part of Ruby on Rails exploration journey.  In this post we will discuss about the Awesome ways to use ruby's Gsub method.

Now, lets first understand the term 'gsub' -> 'sub': Substitute,  'g': global
Sounds a little odd, no problem lets go practically for better understanding.

Lets say we have a string,  str = "Awesome season"

What we want is to replace a word with another in ruby, say in our case it is replacing season with movie,
now what we will do is,

This is saying:
Replace all the occurrences of the first word 'season' with the 'movie'
Well other than just replacement task your gsub method can do a lot more stuffs, let's have a watch.

Replace pattern with regular expression:
So now lets learn to replace not only a single word but a sentences matching a regular expression, such as matching a phone number, email address etc
The first option is the regex (regular expression), that is a pattern matching language
In our case '\d' looks for the numbers, like '2' and '1' in above string.

Switch pattern with regular expression by gsub of ruby:
This worked with the help of 'Capture Groups', in this case we can use the groups as '\1' the first group, and '\2' for the second group. 
Note it that groups in the regex are created by parentheses.

Pass blocks with gsub in ruby:
We all know that how blocks are an important concept and how it reduces our extra work. So why not to use it for gsub, 

We find the course with '\w+', which means one or more alphabetics characters. The logic is self explanatory as you are reading this blog its mean no need to explain the block logic.

Replace a couple of words with another words using hash with gsub in ruby:
Sometimes we want to replace a character with in a string for different characters. Why not to define the replace relation first and then apply gsub once only with the relation for efficient work.

Lets say, we want to replace 'D' with 'dog', 'C' with 'cat' in a string,

As you can also practice above: https://rextester.com/GCCF90557

Ok great, we are done with this post,  

Now if you like my way of talk, lets explore more blogs written by me and my inner knowledge,

Get to know answers for common search on Google : A blog for posts which can help you for daily life problems, such as where to get free images, Topic suggestion for the blog.

Computer Science algorithms and other knowledge share : A blog for posts such as best search algorithm, Top interview questions for diffrent technologies, knowledge share for some frameworks or programming languages for the interview or in general terms.

My ideas to solve real world problems : A blog where me shared and presented my ideas to solve a real world problems, this will be interesting for me.

Future of computer science technology discussed : A blog where me discussed about the future of computer science and new technologies which will change our way for looking to solve problems.

Ruby on Rails Web development Blog : As the name suggest, it is the blog for sharing few knowledge about RoR web development framework.


Liked my blogs, wanna to connect:

LinkedIn   GitHub  |  HackerEarth   ResearchGate  |  Twitter  |  Facebook  |  StackOverflow

Thanks for reading this post, Have a good day :)

 





Comments

Popular posts from this blog

Rails Postgresql Query timeout set and skip during migration

Rails Sync Postgresql and Elasticsearch Database of a Model

Ruby on Rails configure CORS setting with rack cors gem