Ruby Flashcards

1
Q

What does a less_than equal greater_than b return? (Urgh!)

A

-1 if a less than b
0 if a equals b
1 if a greater than b

This is three way comparison and the operator is called the spaceship

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

Naming rule for Classes

A

Start with a capital letter and typically use PascalCase

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

Naming rule for instance variables

A

Start with @ and use lowercase_snake_case

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

Naming rule for class variables

A

Start with @@

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

Naming rule for method names

A

use lowercase_snake_case

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

Naming rule for constants

A

use SCREAMING_SNAKE_CASE

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

What is a Ruby iterator?

A

Simply a method that can invoke a block of code.

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

What is a Ruby block?

A

1) may only appear adjacent to a method call.
2) it is not executed at the time it is encountered. Instead, the block can be invoked from within the method using the yield statement.

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

Describe the action of the yield statement in a method?

A

When a yield is executed, it invokes the code block. When the block exits, control picks back up immediately after the yield.

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

Describe block_given?

A

Returns true if a block is associated with the current method. With block_given, a method can have two different behaviours dependent on whether or not it is called with a block.

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

How do you define a method so that it can receive a code block?

A

If the last parameter in a method definition is prefixed with an ampersand, Ruby looks for a code block whenever that method is called. That code block is converted to an object of class Proc and assigned to the parameter. You can then treat the parameter as any other variable. For example, it can be assigned to an instance variable. The Proc#call method on that object to invoke the block.

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

Thing to remember when coding a numerical literal with an exponential?

A

You must follow the decimal point with a digit. 1.e3 tries to invoke the method e3 in class fixnum.

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

What string method converts a string to an integer?

A

String.to_i

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

How do you can substitute the value of any Ruby expression into a string?

A

Using the sequence #{ expr }. If the expression is just a global variable, a class variable, or an instance variable, you can omit the braces. E.g.

“Seconds/day: #{246060}” » Seconds/day: 86400
“#{‘Ho! ‘*3}Merry Christmas” » Ho! Ho! Ho! Merry Christmas
“This is line #$.” » This is line 3

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