Hash Flashcards

You may prefer our related Brainscape-certified flashcards:
1
Q

What is the use of flatten method in Hash?

A

Returns a new array that is a one-dimensional flattening of self hash

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

What is the difference between delete and delete_if methods?

A

Delete will delete a single entry

delete_if will delete the entries which block conditions evaluates to true.

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

What would be output of the following code?
a = {1=> “one”, 2 => “two”, 3 => “three”, “ii” => “two”}
a.rassoc(“two”)

A

a = {1=> “one”, 2 => “two”, 3 => “three”, “ii” => “two”}

a.rassoc(“two”) #=> [2, “two”]

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

How will you make a duplicate copy of a hash?

A

h.clone

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

What are the ways to represent Ruby hash keys?

A

using symbol h = {:yahoo=>”xxxx”}

using string h ={“yahoo”=>”yyyy”}

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

What is the difference between length and size methods in hash?

A

No difference .they are all alias methods!!

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

to_h,to_a,to_s Please explain these methods?

A

They are used to convert one datatype into another.

Type casting

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

each method is imprtant in hash. What are the other possbile methods with each keyword?

A

each_key
each_value
each_pair

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

How to get a new hash from current hash values as keys, and the keys as values.?

A

invert method

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

In what order are the values of a hash iterated?

A

The order in which they were inserted.

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

Why can you safely use a string as a hash key, even though a string is mutable?

A

Because the interpreter makes a private copy of a string used as a hash key.

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

Does Hash use #== or #eql? to compare hash keys?

A

eql?

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

Write a function that sorts the keys in a hash by the length of the key as a string.
{ abc: ‘hello’, ‘another_key’ => 123, 4567 => ‘third’ } to
[“abc”, “4567”, “another_key”]

A

hsh.keys.collect(&:to_s).sort_by { |key| key.length }

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