Basics Flashcards

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

Ruby’s class for initializing I/O streams.

A

IO

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

Method for iterating through an array and storing the results in a new array

A

Array#collect

ie: array.collect { |elem| elem * 2 }

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

Method for iterating through an array and not storing the results in a new array

A

Array#each

ie: array.each { |item| puts item }

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

String method for breaking up the string into an array at certain points

A

String#split

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

||

this operator means…

A

or

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

&&

this operator means…

A

and

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

number > 0 ? “#{number} is positive” : “#{number} is negative”
this is an example of a ________ expression

A

ternary expression

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

[1, 2, 3][2]

this method will return:

A

3

the 2th index

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

[1, 2, 3].

this method will return:

A

3

the last index

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

Hash method which returns an array of all the hash’s keys

A

Hash#keys

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

method which returns an object’s class

A

Object#class

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

“hello”.is_a? String

what will this return and what is the method doing?

A

true

the method returns a boolean on whether the object is of the argument class

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

For a class to justify its existence, it needs to have two distinct features:

A
  1. State

2. Behavior

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

A ______ is a piece of code that you can store in a variable, and is an object.

A

lambda

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

a piece of code that can’t be stored in a variable and isn’t an object.

A

block

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

_______ only hold behaviour, unlike classes, which hold both behaviour and state.

A

Modules

17
Q

Symbol for the constant lookup operator

A

::

18
Q

What is the scope of a local variable?

A

A local variable is accessible only in the method that it is defined.

19
Q

What is a getter method?

A

A getter method is used within an object. It returns the value of a(n) instance variable(s)

20
Q

What is a setter method?

A

A setter method is used within an object. It can alter the value of a(n) instance variable(s)

21
Q

What is attr_writer?

A

attr_writer is a shortcut for a setter method. It allows the passed instance variable to be changed.

22
Q

What is attr_reader?

A

attr_reader is a shortcut for a getter method/ It allows an instance variable to have its value returned.

23
Q

What is a class variable and what is its notation? And how do changes to a class variable work?

A
Notation: @@ ie: -|@@class_variable|-
It is a variable that is the same for all instances and children of the class in which the class variable is defined. A change to the class variable will be reflected in all instances/children of that class.
24
Q

How accessible are class instance variables?

A

Class instance variables are available only in class methods

25
Q

List the quick summary of RubyMonks review of class variables.

A

Instance variables are available only for instances of a class. They look like @name. Class variables are available to both class methods and instance methods. They look like @@name

It is almost always a bad idea to use a class variable to store state. There are only a very few valid use cases where class variables are the right choice.

Prefer class instance variables over class variables when you do really need store data at a class level. Class instance variables use the same notation as that of an instance variable. But unlike instance variables, you declare them inside the class definition directly.