Ruby Flashcards

(31 cards)

1
Q

What is a class in Ruby?

A

A class is a blueprint for creating objects. It defines the data and behaviors (methods) that its objects will have.

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

How do you define a class in Ruby?

A

Use the ‘class’ keyword followed by the class name (CamelCase)

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

What is an object in Ruby?

A

An object is an instance of a class. Almost everything in Ruby is an object

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

How do you create an object from a class?

A

Call .new on the class: dog = Dog.new

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

What is the initialize method used for?

A

It’s a constructor method that runs automatically when you create a new object. It usually sets up instance variables.

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

How do you define an instance variable?

A

Start with ‘@’ inside a class. Example: @name = "Nick"

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

What is the difference between an instance variable and a class variable?

A

An instance variable (@var) belongs to one object. A class variable (@@var) is shared among all instances of a class.

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

What is a module in Ruby?

A

A module is a collection of methods and constants. It cannot be instantiated but can be included or extended into classes.

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

How do you include a module into a class?

A

Use include ModuleName to add module methods as instance methods.

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

How do you extend a module into a class?

A

Use extend ModuleName to add module methods as class methods.

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

What is ‘Enumerable’ in Ruby?

A

A module that adds powerful iteration methods like map

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

Which Ruby class includes the Enumerable module by default?

A

The Array and Hash classes (and any class that defines an each method).

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

What does the ‘map’ method do?

A

It transforms each element in a collection based on a block and returns a new array of results.

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

What does the ‘select’ method do?

A

It filters elements from a collection based on a condition in the block and returns those that evaluate to true.

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

What does the ‘reduce’ (or ‘inject’) method do?

A

It accumulates a result across elements in a collection

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

What is the difference between ‘each’ and ‘map’?

A

each iterates and returns the original array; map returns a new array with transformed elements.

17
Q

What is a block in Ruby?

A

A block is a chunk of code enclosed in {} or do...end that can be passed to methods for execution.

18
Q

How do you yield to a block inside a method?

A

Use the yield keyword to call the block passed to a method.

19
Q

What happens if you call yield without a block being given?

A

It raises a LocalJumpError unless you check with block_given? first.

20
Q

What are Procs in Ruby?

A

A Proc (procedure) is an object that encapsulates a block of code

21
Q

How do you create a Proc?

A

Use Proc.new { ... } or proc { ... }.

22
Q

How do you call a Proc?

A

Use .call on it

23
Q

What is a lambda in Ruby?

A

A lambda is similar to a Proc but checks arguments strictly and returns control differently.

24
Q

How do you create a lambda?

A

Use lambda { ... } or the shorthand -> { ... }.

25
What is the difference between a Proc and a lambda?
Lambdas check the number of arguments and return to the calling method; Procs don’t check and return from the current scope.
26
How can you pass a block as an argument to another method?
Prefix it with `&` when passing or receiving. Example: `def call_block(&block); block.call; end`
27
How do you make your own class enumerable?
Include the Enumerable module and define an `each` method that yields items.
28
What does 'self' refer to in Ruby?
It refers to the current object instance or the class itself when inside class-level context.
29
What is method chaining?
It’s when you call multiple methods in a single expression
30
How do you define a class method?
Prefix the method name with `self.` inside the class: `def self.say_hi; puts "Hi!"; end`
31
What does attr_accessor do?
It automatically creates both getter and setter methods for an instance variable.