Base concepts/Defenition Flashcards
What can we do if @cached_value ||= some_expression is nil and we can’t use memoization?
if defined?(@cached_value)
@cached_value
else
@cached_value = some_expression
end
What is Set?
Set implements a collection of unordered values with no duplicates.
Set.new([1,2,3,4,4,4,4])
# #
How to create a Struct? and why do we need it?
Struct allows us to create a class in one line.
Artist = Struct.new(:name)
Artist.new(name: ‘Nik’).name
Do we need to use local variables more often? Any advantages?
Local variables allow you to increase performance if we use them instead of other variables.
Is it possible to call a method with 2 possible kinds of argument? ( keyword or ordinary) ?
1
def hello(_x=nil, x: _x)
p x
end
hello(x: 1)
hello(1)
What is Marshal object? And why do we need it?
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.
Give me example of the most difficult argument list in Ruby?
def foo(a, b=”b_default”, *c, d:, e: “e_default”, **f, &g)
# do stuff
end