Array Flashcards
How do you add chain push in an array?
a «_space;1 «_space;2 «_space;3
What does “cycle” method will do in an array?
a.cycle {|x| puts x} # print, a, b, c, a, b, c,.. forever.
Which method is used to do binary search in an array?
bsearch
What is the use of “minmax” method in an array?
Return both minimum and maximum values from an array
a = [[1, [2, 3]]].How will you extract the inner array value 3?
a.dig(0, 1, 1)
How do you remove the nested arrays?
a.flatten
If I want to print the possible combinations(probability) repeatation of the array, which method I can use?
a.permutation.to_a #=> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Differentiate these two methods
a) reverse b)reverse!
First one is notmal method
Second one is Bang method. It will overwrite the original value
Explain take_while method?
a = [1, 2, 3, 4, 5, 0]
a.take_while {|i| i < 3} #=> [1, 2]
Which method will
merges elements of self with corresponding elements from each argument.(index wise)
a = [ 4, 5, 6 ]
b = [ 7, 8, 9 ]
[1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]
How will you search through number of arrays for a specific item?
assoc method
s1 = [ “colors”, “red”, “blue”, “green” ]
s2 = [ “letters”, “a”, “b”, “c” ]
s3 = “foo”
a = [ s1, s2, s3 ]
a.assoc(“letters”) #=> [ “letters”, “a”, “b”, “c” ]
Explain about initialize_copy method
Replaces the contents of self with the contents of other array, truncating or expanding if necessary.
In how many ways items can be removed from array in Ruby?
Ruby array elements can be removed in different ways.
pop
shift
delete
uniq
In how many ways items can be added in an array in Ruby?
Ruby array elements can be added in different ways.
push or «
unshift
insert
What are Ruby arrays and how they can be created?
A Ruby array is created in many ways.
Using literal constructor [] a = [] Using new class method a = Array.new