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:
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
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"
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"]
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
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] ] ]
Empty? :
The ".empty?" method returns the value "true" if the string length is zero.
Ex: a= 10
a.empty? => false
b = “”
b. empty? => true
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"
finite? :
It returns the value "true" if flt is a valid IEEE floating-point number.
Ex: flt.finite? → true or false
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
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
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
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)
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.

Comments
Post a Comment
Comments