Special Methods Examples

 

Here you will be able to find some examples of methods, that might come handy when in need, those are simple examples that will help to understand how to use the method. they are not in any specific order, so I recommend you to just do quick review to see if you find something that can help.


“.times” Block with curly braces

Using the Iteration as a parameter on a variable

“.upto”    Start with 1 instead of 0

“.cycle”

this will execute the instruction on the block “.cycle(x)” where x is equal to the times that the cycle will run.

“.each” <– Can’t Catch

Will execute the block and return the answer for each element without modifying the “Array”

“.collect”

This will return an Array with the values of the block, without modifying the original “Array”

“.Map”

Here we have another method that accomplish the same thing as in the previous example

“.detect”

This one will do exactly what it says, but as soon it finds one value that fulfills the conditional in the block will return that value and skip the rest of the array, even if they are true too.

“.select”

This method will select all the values that fulfills the conditional, and return them, w/m orig Arr.

“.reject”

Opposite to “.select” this method will return elements == false, w/o m orig Arr

“.find”

Will return the FIRST of occurrence that the block is “True”, w/o modifying original Array

“.find_all”

Will return an Array with values when the Block was True, w/o modifying original Array.

“.inject”

This is a fun one and have several applications, here you have two parameters, one will be the accumulator that in this case we name “acc”, but it can be name whatever you want. and “n” this is the one that will iterate each of the elements of the array, Hash, etc.

in the following example we will add all the values together.

“.inject(x)”

here you can also set up acc with a initial value, in the next example we will give it the value of 10 but we will leave all the other things the same so the answer should be 16.

you can return arrays too, in the following example we will initialize the acc as an array by .inject([]) and then in the block we will shovel n*n this means that each number will be multiply by it self and then shovel into the array.

“.reduce”

one use of “.reduce is to sum the elements in an Array” or Multiply them.

“.all?”

This will evaluate all the elements and return true or false depending if all elements fulfills the rules of the method. example:

“.any?”

This will evaluate all the elements and if one or more fulfills the rules of the block will return “true” if none then will return “false”

“.drop”

This will drop the firs x elements

“.drop_while”

This one will delete elements from left to right until one element do no fulfill the block, regardless if other elements on should be drooped.

“.each_slice”

This will slice the array momentarily to mini arrays of x number of elements. w/mod orig arr.

“.group_by”

This one will return a hash where the keys are the product of the computations executed on the block, and the value is an array with the values that have that product.

“.include”

This is not a block but it seems to be useful, basically it will review all the array and return yes if the indicated value is part of the the values on the array.

“.max”

here we have an example where we create an array of words, then we print the max when max means the highest indice, then we print the max but we modify it with a block that will chose the the max in terms of highest number of characters.

here is the same example but modify to provide the two longest strings.

here is one variation here we will find the longest string

“.min”

Its name says it all

“.minmax”

Another way to get them by the length is

“.charts”

will create an array from a string

“.count”

it can also count occurrences in a “String”

“benchmark” —  “:bsearch”

“.index(x)”

“.gsub”

Returns a copy of str with the all the occurrences of a patter substituted

It also can add extra characters or spaces in between

“.flatten”

This will take a TWO dimensional array and flatten it to a ONE dimensional array.

It can also regulate what dimensions to flatten, the next example is a 3rd dimensional Array where we flatten only one of the two sub-dimensions.

 

 

line

 



Copyright 2017. All rights reserved.

Posted December 30, 2016 by Edmundo in category "Ruby