Ruby On Rails Built-In Methods

Total Blog Views: 38

Blog Status: publish

Created By: swazahmad Created at: 08-05-2021

Tags: basicprogramming ruby on rails

The Ruby On Rails Provide The huge kernal libraries so we do not need to Write the n number Lines of code to acheived the Scope of works

include? :

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

a = [ "apple", "dog", "cat"]

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

flatten:

It returns a replacement array within the sort of one-dimensional flattening of the array (recursively). That is, for each element present, it extracts its elements into a replacement array.

The Above Method is basically use to remove brackets from array of array in ruby.

a = [ 22, 31, [14, [55, 6] ] ]

a.flatten ==> [22, 31, 14, 55, 6]

collect():

The collect() of enumerable is an inbuilt method in Ruby returns a replacement array with the results of running block once for each element in enum. the thing is repeated whenever for every enum. just in case no object is given, it return nil for every enum.

a = [ "apple", "ball", "cat", "dog"]

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

["apple!", "ball!", "cat!", "dog!"] 

join() :

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

[ "1", "2", "3" ].join => "123"

[ "1", "2", "3" ].join(",") => "1,2,3"

uniq() :

As Method defined meaning Itself Provide a new unique array by Removing duplicate entries.

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

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

finite? :

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

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.

1.4.round → 1

sum :

Sum is used to add the all entries into On we can use sum method in several ways in active record, array, sum of two string.

[1,2,3].sum → 6

 

pluck :

The Pluck methods return an Array of records we requested for example, if we need to pluck name from users Table we can write below.

User.pluck(:name) ===> ['sample 1', 'sample 2']

empty:

Empty Method is basically use to check the object is having the value or not.

a = 'swaz'

a.empty? => false

b = ''

b.empty? => true

gsub:

The gsub method replaces every reference of the primary parameter with the second parameter within the string.

"We are programmer".gsub("are", "are good")

"We are good programmer"

Conclusion :

With several ruby methods available, within the above-mentioned ways, these methods are often called and executed
Thank you so much for Reading
 .


swazahmad

Swaz is Practice Lead of ROR Developer. he's a Blogger, Mentor, and Rubyst. He has expertise in Javascript also . he's highly skilled in designing, developing and testing web applications. He enjoys being challenged and interesting with projects that need him to



Comments



  • swazahmad | almost 4 years ago
    Awesome Information Thanks For Sharing
Buy traffic for your website

About Shadbox

we have the “Get things executed” lifestyle at our place of work. There are not any excuses, no if’s or however’s in our dictionary. committed to navigating the ship of creativity to create cell answers, we resolve the real-lifestyles troubles of our clients and their clients. Our passion for work has won us many awards, year after 12 months.

Services

Downloads