Ruby Quiz - 11.2.2021 Flashcards
a = [[4, [5, 8]]].How will you extract the inner array value 8?
a.dig(0, 1, 1)
Which method will merge elements of curremt array 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 make a new copy of a current hash?
h.clone
Which method rebuilds the hash based on the current hash values for each key. If values of key objects have changed since they were inserted?
rehash method will reindex hash. a = [ "a", "b" ] c = [ "c", "d" ] h = { a => 100, c => 300 } h[a] #=> 100 a[0] = "z" h[a] #=> nil h.rehash #=> {["z", "b"]=>100, ["c", "d"]=>300} h[a] #=> 100
differentiate - “hex’ and “oct” methods in String
hex - Treats leading characters from str as a string of hexadecimal digits
“0x0a”.hex #=> 10
“-1234”.hex #=> -4660
oct-Treats leading characters of str as a string of octal digits
“123”.oct #=> 83
“-377”.oct #=> -255
Which method is used to check if the string is invalid byte sequence then replace invalid bytes with given replacement character,
scrub method
“abc\u3042\x81”.scrub(“”) #=> “abc\u3042”
Is everything in Ruby an object?
Methods are not objects. Blocks are not objects. Keywords are not objects. However, there exist Method objects and Proc objects, and some keywords refer to objects.
What are the subclasses of numeric datatype?
Integer,Float,BigDecimal,Complex,Rational
if a symbol (say :Friend) is a constant in one context, a method in another, and a class in a third, that :ruby will be the same object in all three contexts. True or false?
module One class Friend end $f1 = :Friend end module Two Fred = 1 $f2 = :Friend end def Friend() end $f3 = :Friend $f1.object_id #=> 2514190 $f2.object_id #=> 2514190 $f3.object_id #=> 2514190
What does “cycle” method will do in an array?
a.cycle {|x| puts x} # print, a, b, c, a, b, c,.. forever.
How can you do chain push in an array?
a = []
a «_space;1 «_space;2 «_space;3
What are the mehods to check hash keys?
Hash#has_key?, Hash#include?, Hash#member?
Is ther any class in the name "Boolean" ? What are the classes that support boolean data type?
No,True class, flase class
What “quo” method will do in Rational?
It will perform division
Explain pred and succ methods in Numeric?
n. pred will print preceeder
n. succ will print successor