Ruby: Built-In Methods
By:Category: Ruby on Rails Technologies: Ruby on Rails
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
2. 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”
3. 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”]
4. 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
5. 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] ] ]
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
7. 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”
8. finite? :
It returns the value “true” if flt is a valid IEEE floating-point number.
Ex: flt.finite? → true or false
9. 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
10. 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
11. 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
12. 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)
13. 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.
Originally published at https://www.cryptextechnologies.com.