Ruby: Built-In Methods

Ruby: Built-In Methods

13 Jul 2021
0 Comments
Ruby: Built-In Methods

The Kernel module is incorporated by Object class, which makes its methods available throughout the Ruby program. They can be called without a receiver or functional form. Hence, they are often referred to as functions. With several built-in methods available in Ruby, they help to reduce manual calculations and lines of code and thereby helps to increase the performance and speed of the application. Some of the pre-defined methods are as mentioned below:

 

  1. Collect :

This function returns a new array by passing each element as a parameter to block and invoking a block for every element. The generated result is then used as the given element in a new array.

Ex: a = [ "a", "d", "b", "e"]

a.collect {|x| x + "!" }

a

 

  1. join() :

The join() function returns a string created by converting every element of the array to a string and separates them by a SepString.

Ex: [ "a", "b", "c" ].join => "abc"

[ "a", "b", "c" ].join("-") => "a-b-c"

 

  1. uniq() :

This function returns a new array by removing all the duplicate values in arr.

Ex: a = [ "a", "a", "b", "b", "c" ]

a.uniq => ["a", "b", "c"]

 

  1. include? :

This function returns the value "true" if the given object is present in arr [(that is, if any object == anObject), false otherwise].

Ex: a = [ "a", "k", "q"]

a.include?("k") => true

a.include?("z") => false

 

  1. flatten :

It returns a new array in the form of one-dimensional flattening of the array (recursively). That is, for every element present, it extracts its elements into a new array.

Ex: a = [ 2, 3, [4, [5, 6] ] ]

 

  1. Empty? :

The ".empty?" method returns the value "true" if the string length is zero.

Ex: a= 10

a.empty? => false

b = “”

b. empty? => true

 

  1. Gsub :

The gsub method replaces every reference of the first parameter with the second parameter in the string.

Ex: "ruby is cool".gsub("cool", "very cool") #=> "ruby is very cool"

 

  1. finite? :

It returns the value "true" if flt is a valid IEEE floating-point number.

Ex: flt.finite? → true or false

 

  1. infinite? :

It returns the value 'nil', '-1', or '+1' depending on whether flt is 'finite', '-infinity', or '+infinity'.

(0.0).infinite? → nil

(-1.0/0.0).infinite? → -1

(+1.0/0.0).infinite? → 1

 

  1. round :

It returns a number rounded to the nearest integer or equivalent to.

def round

return floor(self+0.5) if self > 0.0

return ceil(self-0.5) if self < 0.0

return 0.0

end

Ex: 1.5.round → 2

 

  1. sum :

It returns a basic n-bit checksum of the characters in the str, where n is the optional parameter, defaulting to 16. The result is the sum of the binary value of each character in (str modulo 2n - 1). This is not a good checksum.

Ex: str.sum(aFixnum=16 ) → anInteger

 

  1. pluck :

The pluck function returns the array and splits the ActiveRecord query method chain. Hence it is suggested to be careful when the query is called on the result.

Ex: User.where(name: “ABC”).pluck(:id)

 

  1. Convert string array to integer array and generating the sum of the elements :

Ex: quizz_score = [“20” “30” “50”]

quizz_exam_percentage = quizz_score.map(&:to_i).sum

quizz_exam_percentage = 100

 

Conclusion :

With several ruby methods available, in the above-mentioned ways, these methods can be called and executed.




Leave a comment: