Ruby Quiz - 11.2.2021 Flashcards

1
Q

a = [[4, [5, 8]]].How will you extract the inner array value 8?

A

a.dig(0, 1, 1)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Which method will merge elements of curremt array with corresponding elements from each argument.(index wise)

A
a = [ 4, 5, 6 ]
b = [ 7, 8, 9 ]

[1, 2, 3].zip(a, b) #=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How will you make a new copy of a current hash?

A

h.clone

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

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?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

differentiate - “hex’ and “oct” methods in String

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which method is used to check if the string is invalid byte sequence then replace invalid bytes with given replacement character,

A

scrub method

“abc\u3042\x81”.scrub(“”) #=> “abc\u3042

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Is everything in Ruby an object?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What are the subclasses of numeric datatype?

A

Integer,Float,BigDecimal,Complex,Rational

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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?

A
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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What does “cycle” method will do in an array?

A

a.cycle {|x| puts x} # print, a, b, c, a, b, c,.. forever.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you do chain push in an array?

A

a = []

a &laquo_space;1 &laquo_space;2 &laquo_space;3

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What are the mehods to check hash keys?

A

Hash#has_key?, Hash#include?, Hash#member?

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q
Is ther any class in the name 
"Boolean" ? What are the classes that support boolean data type?
A

No,True class, flase class

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What “quo” method will do in Rational?

A

It will perform division

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Explain pred and succ methods in Numeric?

A

n. pred will print preceeder

n. succ will print successor

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

How to display the inbetween values in a range?

A

entries, to_a

17
Q

difference between last and max methods in Range?

A
  1. .10.last= “10”

1. ..10.max = “9”

18
Q

What each_pair method will do? Which class it belongs to?

A

take the pair values from a hash. Hash class

19
Q

Can case statement be assigned to any variable?

A

yes. This value can be assigned to a variable preceding the case statement.

happy = case

when …

end

puts happy

20
Q

How can you resolve name conflict(same name in mixin modules) in modules?

A

alias_method.

module Happy

	def expression
		return "smiling"
	end
	alias happyexpression expression
end