Ruby methods cheat sheets(.class, array, flatten)

tamilz
May 10, 2021

--

To find the type of variable or objects: add .class

find the class of the variable or objects

Ruby array methods

map => return new array elements

a = [1,2,3]

a.map {|a| a*2 }

[2,4,6]

select => filter the array elements with condition

a= [1, 2,3,5,6]

a.select { |a| a.even? }

return only even numbers

a = [2, 6]

flatten methods

array inside array a = [1,2,3 [1,5]]

a.flatten => [1,2,3,5]

add flatten! or save!

it will instanly assign to itself

here we discussing push(), pop(), .uniq

a = [1,1,2,3,4,4]

add .uniq to array element

a.uniq

a = [1,2,3,4]

return only unique value

in ruby we can push multiple elements using

a.push(5,6,7)

[1,2,3,4,5,6,7]

a.pop()

remove the last the elements of the array

--

--

No responses yet