RUBY (part-4)

RUBY (part-4)

Introduction

In the previous blog, we learned about the general basic programming concepts from this onwards let's dig into some new and well-mature topics.

unless

Guys!!! pronounce it correctly it's not useless.

Jokes apart in the previous blog we learned about the control flow, it is just an extension of that. In simple words, we can say it is the reverse of the if condition.

The code inside the if statement only executes only when the if condition is true. In another case, it executes the else condition if we define it.

Wouldn't it be very cool if the code inside the condition only executes when the condition is FALSE? Ruby is letting us do that. It's as simple as that. let's try to code this.

a = 10
unless a == 11 #False
    print "This is executing because of unless statement"
end
output:
This is executing because of unless statement

Sorry Readers

As we all know that operators play a very major role in coding and skipped the entire part. But there is a reason behind this as a developer we have at least gone through a programming language that too everyone experience operator concepts definitely. Probably this is the reason why I did. In ruby, the operators are also the same, and their work is also.

If you are new to programming you just started then no worries. I am assuring you operators is a simple concept. I am giving you the link below just go through it once.

Click Here To Navigate the Operator Concept

String methods

In every programming language, there are some inbuilt methods will be there to save developers time and Ruby has too. Till now we only work with string data types the most. Let's look at a few string methods we use often.

Note: There are so many string methods & I am not gonna discuss all of them.

The methods are called by using the "." operator.

Let's look at a few string methods direct usecase with the help of code

string = "Malavi"
puts string.length  #returns the length of the string
puts string.downcase  #converts the whole string into lowercase and returns the copy of that
puts string.upcase  #converts string into uppercase
puts string.capitalize #capitalizes the first letter of string
puts string.index("a") #returns the index value where starting of the string found
puts string.size #same as length function
output:
6
malavi
MALAVI
Malavi
1
6

Special Methods

I name a few methods as special because while working I found their function a little bit tricky.

Global substitution methods (gsub or gsub!)

The whole point of gsub method in strings is to replace part of a string. Used to return a copy of the given string with all occurrences of pattern substituted for the second argument.

Here g --->stands and sub-->stands for substitution

Placing "!" after the method means ruby will change the string in place. This applies to a few string methods also like upcase! downcase! and capitalize! likewise.

The syntax for this is a string.gsub(pattern, replacement) this indicates that it took 2 arguments.

In the first place(pattern) we have to specify which we want to remove.

In the second place (replacement) we have to specify which we want to replace.

By using the above knowledge try to make a little substitution in the given string.

name = "Malavi"
puts name.gsub(/a/, "o")# it replaces all a's with o
output:
Molovi #haha

Note: The first argument must be in between this "//" .Just syntax

While learning about this method I found it has so many advanced use cases with advanced syntax if you want to just click below

gsub advanced

include ?

Returns true if a string is included in another string, Otherwise returns false.

In general ruby methods that end with "?" evaluates(returns) boolean values (true or false)

name "Malavi"
puts name.include? "a"
output:
true

Padding a string

Hold on !! This padding is not CSS padding because we don't have any choice but to add something to the above and below. Instead, we have a chance to add something on the left and right.

rjust,ljust and center

All three methods return a string of a given length by adding Unicode characters on the left, right and center respectively. They took two arguments for the process.

Note: The first argument should be an integer and the second argument should be a Unicode character.

let's look at the code snippet once

name = "Malavi"
puts name.ljust(10,"*")
puts name.rjust(10,"*")
puts name.center(10,"*")
output:
Malavi****
****Malavi
**Malavi**

The keynote here is we are getting padding on the right side for the ljust method and padding on the left side for the rjust method.

Stript

This method lets us remove the white space in the string. We can even more particularly remove the left side and right side space of our choice.

Let's try to code that

name = "    Malavi    "
puts name.strip
puts name.rstrip #removes the rightside space
puts name.lstrip #removes the leftside space
output:
Malavi
____Malavi
Malavi____
# In the output I use underscore to represent spaces

Conclusion

This is all about the blog readers. There are so many inbuilt methods and we came to know all of them while doing hands-on practice. I will come up with another blog on iterations in ruby. Hope you learn something new in ruby. Let me how the blog is in the comment below. Happy learning!!!

Did you find this article valuable?

Support Malavi Pande by becoming a sponsor. Any amount is appreciated!