Base concepts/Defenition Flashcards

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

What can we do if @cached_value ||= some_expression is nil and we can’t use memoization?

A

if defined?(@cached_value)
@cached_value
else
@cached_value = some_expression
end

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

What is Set?

A

Set implements a collection of unordered values with no duplicates.

Set.new([1,2,3,4,4,4,4])
# #

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

How to create a Struct? and why do we need it?

A

Struct allows us to create a class in one line.

Artist = Struct.new(:name)
Artist.new(name: ‘Nik’).name

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

Do we need to use local variables more often? Any advantages?

A

Local variables allow you to increase performance if we use them instead of other variables.

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

Is it possible to call a method with 2 possible kinds of argument? ( keyword or ordinary) ?

A

1

def hello(_x=nil, x: _x)
p x
end

hello(x: 1)
hello(1)

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

What is Marshal object? And why do we need it?

A

In Ruby, the Marshal module provides functionality for serializing and de-serializing Ruby objects. When you serialize an object, you convert it to a byte stream, which can be stored in a file or transmitted over a network. When you de-serialize the object, you convert it back into a Ruby object.

One reason to use Marshal is to store complex objects in a file or database, so that they can be retrieved and used later. For example, you might use Marshal to save the state of a game, so that the player can resume playing from where they left off.

Another reason to use Marshal is to transmit complex data structures between processes or systems. For example, you might use Marshal to send an object from a server to a client over a network.

Overall, Marshal is a useful tool for storing and transmitting complex data structures in Ruby. It is particularly useful when you need to store or transmit data that cannot be easily represented as a simple data type such as a string or a number.

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

Give me example of the most difficult argument list in Ruby?

A

def foo(a, b=”b_default”, *c, d:, e: “e_default”, **f, &g)
# do stuff
end

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